bash: How to reuse the search result of "find"


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting bash: How to reuse the search result of "find"
# 1  
Old 03-14-2012
Question bash: How to reuse the search result of "find"

Code:
find . -type f -print0 | xargs -0 chmod 600

find . -type f


On bash, I would like to pass the search result of "find" to another command as well as to the standard output. The above code performs the same search twice -- once for "xargs -0 chmod" and another for stdout. I would like to spare this redundancy. I would like to search only once and would like to use the search result for another command and reuse it for the standard output.

Assume that the null character (0x00) is the only character that filenames never contain. Thus, assume that filenames may contain newline (0x0A) characters. In the above code, -print0 and -0 options specify the separator to be a null character.

If I allow myself to use a file to save the search result, then the work can be accomplished in the following manner.

Code:
find . -type f -print0 > /tmp/found.dat
cat /tmp/found.dat | xargs -0 chmod 600
cat /tmp/found.dat | tr \\0 \\n

However, I do not want to use a file to save the search result. So, I tried a variable to save the search result.

Code:
vFound="$(find . -type f -print0)"
echo "$vFound" | xargs -0 chmod 600
echo "$vFound" | tr \\0 \\n
#or 
#printf %s "$vFound" | xargs -0 chmod 600
#printf %s "$vFound" | tr \\0 \\n

However, the above code failed. It seems that bash removes null characters when bash expands the variable.

I also tried "tee" to make an attempt to split the search result to "xargs -0 chmod" and stdout. The following attempt with "tee" failed.

Code:
find . -type f -print0 | tee - | xargs -0 chmod 600

Can you show me how to use the search result of "find" for another command and reuse it for the standard output without saving the search result to a file? I failed in a method with a variable and a method with "tee".

Many thanks in advance.
# 2  
Old 03-14-2012
You could do this with a named pipe:

Code:
mkfifo mypipe
something_else_using mypipe &
find . -type f -print0 | tee mypipe | xargs -0 chmod 600

Something else has to be trying to read from mypipe before tee can write to it, otherwise, it will block until it can.
# 3  
Old 03-15-2012
Quote:
Originally Posted by Corona688
You could do this with a named pipe:

Code:
mkfifo mypipe
something_else_using mypipe &
find . -type f -print0 | tee mypipe | xargs -0 chmod 600




Thanks Corona688 for reminding me of named pipes. However, the statement "mkfifo mypipe" creates a file "mypipe" in the current directory. I do not want to use any files explicitly. So, instead of Corona688's explicit named pipe, I have ended up with the following implicit named pipe.

Code:
find . -type f -print0 | tee >(xargs -0 chmod 600) | tr \\0 \\n


The greater-than symbol followed by a subshell is the implicit named pipe. Implicit named pipes do not need "mkfifo" statements. This single-line solution is successful and satisfies my initial demand.

By the way, the purpose of "tr \\0 \\n" is to convert back the null-char-delimited result into newline-delimited data for stdout. I might further modify the tr statement to the following so that possible newline characters in filenames be represented by question marks.

tr \\n\\0 ?\\n
# 4  
Old 03-19-2012
If I need to use (or reuse) the search result later but not immediately following "find", I would like to save it into a variable. However, the search result contains null characters as separators, and bash removes null characters either upon variable assignment or upon variable expansion. To preserve null characters, the percent-encoding is useful.

Percent encoding is widely used for URL encoding. The following method applies percent-encoding to only the null character and the percent symbol itself.

Code:
#!/bin/bash
#percent-encoding for null character
PercentizeNull()
{
  sed "s/%/%25/g" | sed "s/\x0/%00/g"
}
DepercentizeNull()
{
  sed "s/%00/\x0/g" | sed "s/%25/%/g"
}
echo -ne "A\0B\0%\0" | hexdump -C
foo=$(echo -ne "A\0B\0%\0" | PercentizeNull)
printf %s "$foo" | DepercentizeNull | hexdump -C



The following example saves the search result of "find" into a variable with percent-encoding.

Code:
PercentizeNull()
{
  sed "s/%/%25/g" | sed "s/\x0/%00/g"
}
DepercentizeNull()
{
  sed "s/%00/\x0/g" | sed "s/%25/%/g"
}

vFound=$(find . -perm /o+rwx ! -type l -print0 2> /dev/null | PercentizeNull)

if test "$vFound" ; then
  echo "current permission"
  printf %s "$vFound" | DepercentizeNull | xargs -0 ls -ld
  printf %s "$vFound" | DepercentizeNull | xargs -0 chmod o-rwx
  echo "new permission"
  printf %s "$vFound" | DepercentizeNull | xargs -0 ls -ld
fi



By the way, does anyone know at which time bash removes null characters, at the time of variable assignment or at the time of variable expansion? Which time? Does bash never save null characters into a variable? Or, does bash save null characters into the variable, and does bash remove null characters when the variable is expanded?

Furthermore, without any encoding (such as percent encoding), is there any option to prevent bash from removing null characters?

Last edited by LessNux; 03-19-2012 at 05:28 PM..
# 5  
Old 03-19-2012
I think it's at assignment:

Code:
$ T=$(printf "A\x0B")
$ echo ${#T}
2
 
$ T=$(echo "AAA" | sed "s:A:\x0:"g)
$ echo ${#T}
0

I suspect bash uses the standard C string functions to deal with it's env vars, and they use NULL as a terminator. One could imagine that re-implementing a new string object that supported imbedded NULL chars wouldn't be worth the effort.


BTW, the contents of named pipes aren't stored in the Filesystem (the file is only a pointer, much like a /dev/ file, the actual data is stored in memory).

Also, how about using the -e sed param in PercentizeNull() and DepercentizeNull() to reduce the number of sed processes run:
Code:
PercentizeNull()
{
  sed -e "s/%/%25/g" -e "s/\x0/%00/g"
}


Last edited by Chubler_XL; 03-19-2012 at 06:07 PM..
This User Gave Thanks to Chubler_XL For This Post:
# 6  
Old 03-20-2012
Quote:
Originally Posted by Chubler_XL
I think it's at assignment:

I suspect bash uses the standard C string functions to deal with it's env vars, and they use NULL as a terminator. One could imagine that re-implementing a new string object that supported imbedded NULL chars wouldn't be worth the effort.


Also, how about using the -e sed param in PercentizeNull() and DepercentizeNull() to reduce the number of sed processes run:
Code:
PercentizeNull()
{
  sed -e "s/%/%25/g" -e "s/\x0/%00/g"
}



Thanks, Chubler_XL.

It was indeed awkward of my code to invoke "sed" consecutively multiple times. The subroutines should be polished as you suggested.

Code:
#percent-encoding for null character
PercentizeNull(){
  sed -e "s/%/%25/g" -e "s/\x0/%00/g"
}
DepercentizeNull(){
  sed -e "s/%00/\x0/g" -e "s/%25/%/g"
}



In addition, the following method with semicolon also spares consecutive multiple invocations of "sed".

Code:
#percent-encoding for null character
PercentizeNull(){
  sed 's/%/%25/g ; s/\x0/%00/g'
}
DepercentizeNull(){
  sed 's/%00/\x0/g ; s/%25/%/g'
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. Shell Programming and Scripting

find . -path "*_nobackup*" -prune -iname "*.PDF" \( ! -name "*_nobackup.*" \)

These three finds worked as expected: $ find . -iname "*.PDF" $ find . -iname "*.PDF" \( ! -name "*_nobackup.*" \) $ find . -path "*_nobackup*" -prune -iname "*.PDF" They all returned the match: ./folder/file.pdf :b: This find returned no matches: $ find . -path "*_nobackup*" -prune... (3 Replies)
Discussion started by: wolfv
3 Replies

3. UNIX for Dummies Questions & Answers

"Help with bash script" - "License Server and Patch Updates"

Hi All, I'm completely new to bash scripting and still learning my way through albeit vey slowly. I need to know where to insert my server names', my ip address numbers through out the script alas to no avail. I'm also searching on how to save .sh (bash shell) script properly.... (25 Replies)
Discussion started by: profileuser
25 Replies

4. Shell Programming and Scripting

Find lines with "A" then change "E" to "X" same line

I have a bunch of random character lines like ABCEDFG. I want to find all lines with "A" and then change any "E" to "X" in the same line. ALL lines with "A" will have an "X" somewhere in it. I have tried sed awk and vi editor. I get close, not quite there. I know someone has already solved this... (10 Replies)
Discussion started by: nightwatchrenba
10 Replies

5. Shell Programming and Scripting

Using a single "find" cmd to search for multiple file types and output individual files

Hi All, I am new here but I have a scripting question that I can't seem to figure out with the "find" cmd. What I am trying to do is to only have to run a single find cmd parsing the directories and output the different file types to induvidual files and I have been running into problems.... (3 Replies)
Discussion started by: swaters
3 Replies

6. Shell Programming and Scripting

grep with "[" and "]" and "dot" within the search string

Hello. Following recommendations for one of my threads, this is working perfectly : #!/bin/bash CNT=$( grep -c -e "some text 1" -e "some text 2" -e "some text 3" "/tmp/log_file.txt" ) Now I need a grep success for some thing like : #!/bin/bash CNT=$( grep -c -e "some text_1... (4 Replies)
Discussion started by: jcdole
4 Replies

7. Shell Programming and Scripting

Simplify Bash Script Using "sed" Or "awk"

Input file: 2 aux003.net3.com error12 6 awn0117.net1.com error13 84 aux008 error14 29 aux001.ha.ux.isd.com error12 209 aux002.vm.ux.isd.com error34 21 alx0027.vm.net2.com error12 227 dux001.net5.com error123 22 us008.dot.net2.com error121 13 us009.net2.com error129Expected Output: 2... (4 Replies)
Discussion started by: sQew
4 Replies

8. Shell Programming and Scripting

BASH find filenames in list that match certain "pattern."

I guess by "pattern," I mean something different from how that word is defined in the Linux world. If you take $ to mean a letter (a-z) and # to mean a number (0-9), then the pattern I'm trying to match is as follows: $$$##-####-###-###.jpg I'd like to write a script that reads in a list of files... (4 Replies)
Discussion started by: SilversleevesX
4 Replies

9. UNIX for Dummies Questions & Answers

"find": search the a regex of FILES??

I want to use the find command to search a ton of files, but I want to break it up into multiple machines. I want to search for files with "filename." in the title. The location I want to search is: /u/*/*/*/stuff On the first computer I want to search: /u//*/*/stuff Right now I am doing... (1 Reply)
Discussion started by: msf5042
1 Replies

10. Shell Programming and Scripting

"find command" to find the files in the current directories but not in the "subdir"

Dear friends, please tell me how to find the files which are existing in the current directory, but it sholud not search in the sub directories.. it is like this, current directory contains file1, file2, file3, dir1, dir2 and dir1 conatins file4, file5 and dir2 contains file6,... (9 Replies)
Discussion started by: swamymns
9 Replies
Login or Register to Ask a Question