I need to substitute output of find command to another command to process every found file, e.g.:  mdls `find ~/target_dir/ -iname '*some*' -depth 1`   (mdls is a command in OS X which get metadata attributes of the specified file. It doesn't support piping thus find ... | mdls fails.)  The command above works fine, but expectedly fails on files with spaces in name. I thought that adding quotes via sed helps with it:  $ find ~/target_dir/ -iname '*some*' -depth 1 | sed 's/\(.*\)/"\1"/' "/Users/shau-kote/target_dir//secondsomefile" "/Users/shau-kote/target_dir//some file with spaces in name" "/Users/shau-kote/target_dir//somefile"   Alack, now my mdls command fails on all files:  $ mdls `find ~/target_dir/ -iname '*some*' -depth 1 | sed 's/\(.*\)/"\1"/'` "/Users/shau-kote/target_dir//secondsomefile": could not find "/Users/shau-kote/target_dir//secondsomefile".   How can I fix it so as mdls do correctly process all file names from find?  Thank you.  P.S. I'm not sure is it important or self-evident, but   mdls "/Users/shau-kote/target_dir//secondsomefile"   works properly.
1847
Or, an introductory guide to robust filename handling and other string passing in shell scripts.  I wrote a shell script which works well most of the time. But it chokes on some inputs (e.g. on some file names).  I encountered a problem such as the following:   I have a file name containing a space hello world, and it was treated as two separate files hello and world. I have an input line with two consecutive spaces and they shrank to one in the input. Leading and trailing whitespace disappears from input lines. Sometimes, when the input contains one of the characters \[*?, they are replaced by some text which is is actually the name of files. There is an apostrophe ' (or a double quote ") in the input and things got weird after that point. There is a backslash in the input (or: I am using Cygwin and some of my file names have Windows-style \ separators).   What is going on and how do I fix it?
47242
I am running the following command, but it is not performed recursively:  find . -name *.java   I know there are java files further down in the current directory but it is performing the find on the current directory only. I am using OS X, 10.9.
65920
I have a input file FILE1.TXT as below.     11 id1   12   13 AGE = 20   14 NAME = NAME1   15   16 id2   17   18 AGE = 30   19 NAME = NAME2   .   .   .   110 idXYZ   111   112 AGE = AGEXYZ   113 NAME = NAMEXYZ   114   115 idZZZ   116     I want to search all the fields that belong to a particular Id and get the value for NAME  I managed to loop through each Id and formed the below command for each Id as required.  sed -n '/11/,/14/p' FILE1.TXT | grep NAME | awk -F "= " '{print $2}'  The problem here is, I get the output NAME1, in addition to that, I also get NAMEXYZ.  What should be changed so that I only get NAME1 but not NAMEXYZ?  As a workaround, the below commands work.  sed -n '/11/,/14/p' FILE1.TXT | grep NAME | awk -F "= " '{print $2}'|head -1  Is there any 'switch' or am I missing something?
22924
How can I search and replace text that contains / with sed?  I currently use the following command which doesn't work  sed -i "s/queue_directory = /var/spool/postfix-secondary/queue_directory = /var/spool/postfix-$newnumber/g" /etc/postfix-$newnumber/main.cf
40404
Very new to UNIX but not new to programming. Using Terminal on MacBook. For the purposes of managing and searching word lists for crossword construction, I'm trying to get handy with the Grep command and its variations. Seems pretty straightforward but getting hung up early on with what I thought should be a simple case.  When I enter   grep "^COW" masternospaces.txt   I get what I want: a list of all the words starting with COW.  But when I enter   grep "COW$" masternospaces.txt   I expect to get a list of words ending with COW (there are many such words), and nothing is returned at all.   The file is a plain text file, with every line just a word (or a word phrase with no spaces) in all caps.  Any idea what could be happening here?
51790
I have a bash shell script that loops through all child directories (but not files) of a certain directory.  The problem is that some of the directory names contain spaces.    Here are the contents of my test directory:  $ls -F test Baltimore/  Cherry Hill/  Edison/  New York City/  Philadelphia/  cities.txt   And the code that loops through the directories:  for f in `find test/* -type d`; do   echo $f done   Here's the output:   test/Baltimore test/Cherry Hill test/Edison  test/New York City test/Philadelphia   Cherry Hill and New York City are treated as 2 or 3 separate entries.  I tried quoting the filenames, like so:  for f in `find test/* -type d | sed -e 's/^/\"/' | sed -e 's/$/\"/'`; do   echo $f done   but to no avail.  There's got to be a simple way to do this.    The answers below are great.  But to make this more complicated - I don't always want to use the directories listed in my test directory.  Sometimes I want to pass in the directory names as command-line parameters instead.  I took Charles' suggestion of setting the IFS and came up with the following:  dirlist="${@}" (   [[ -z "$dirlist" ]] &amp;&amp; dirlist=`find test -mindepth 1 -type d` &amp;&amp; IFS=$'\n'   for d in $dirlist; do     echo $d   done )   and this works just fine unless there are spaces in the command line arguments (even if those arguments are quoted).  For example, calling the script like this: test.sh "Cherry Hill" "New York City" produces the following output:   Cherry Hill New York City
115643
If I just use basename {} .txt, it will work:  find . -iname "*.txt" -exec basename {} .txt \;   It will just print xxx instead of ./xxx.txt  if I want use $(basename {} .txt) in the -exec option, it will fail:  find . -iname "*.txt" -exec echo "$(basename {} .txt)" \;   It will just print ./xxx.txt  How can I solve this problem? I hope I can use $(basename {} .txt) as parameters for other cmd. Do I have to do sh -c or pipe -exec basename {} \; with xargs?
54821
I know this question has probably been answered before.  I have seen many threads about this in various places, but the answers are usually hard to extract for me.  I am looking for help with an example usage of the 'sed' command.  Say I wanted to act upon the file "hello.txt" (in same directory as prompt).  Anywhere it contained the phrase "few", it should be changed to "asd".  What would the command look like?
39072
I'm trying to merge the output of 2 find commands and pipe it to the next operator. The arguments passed to both find executions are very similar so I'd like to keep it DRY  FIND_CMD_ARGS="-type f \( -iname \*.m -o -iname \*.swift \) -print0" CMD_OUTPUT=`{ find $PROJECT_PATH -not -path '*/Pods/*' $FIND_CMD_ARGS; find . -path '*/Pods/MyProject*/*' $FIND_CMD_ARGS }`   For some reason this doesn't work and this is the output I get  find: illegal option -- n usage: find [-H | -L | -P] [-EXdsx] [-f path] path ... [expression]        find [-H | -L | -P] [-EXdsx] -f path [path ...] [expression] find: -type f \( -iname \*.m -o -iname \*.swift \) -print0: unknown primary or operator   I think the issue is related to how the args in the FIND_CMD_ARGS environment variable are escaped but I can't figure out the right way to do it.  Any ideas? Thanks!
19557
I would like to replace $fileWithPath in $file, however this doesn't work because (I think) path is not escaped. How to escape it?  sed -i 's/${fileWithPath}/HAHA/g' $file
25827
I needed to find all the files that contained a specific string pattern. The first solution that comes to mind is using find piped with xargs grep:  find . -iname '*.py' | xargs grep -e 'YOUR_PATTERN'   But if I need to find patterns that spans on more than one line, I'm stuck because vanilla grep can't find multiline patterns.
4911
I started learning linux on vmware using centos 7. I created an image directory and several layers of files and sub-directories in that folder. most of the names containing spaces. I want to use a single command to rename all the files and directories at once.   command i am using right now is  find . -type f -exec rename "find" "replace" {} \; &amp; find . -type d -exec rename "find" "replace" {} \; Where as find is a space and replace is "-"(hiphen). I even tried below command looking at one of the answer in stack exchange.  find . -iname "find" -exec rename "find" "replace" {} \;
72476
My current best bet is:  for i in $(find . -name *.jpg); do echo $i; done   Problem: does not handle spaces in filenames.   Note: I would also love a graphical way of doing this, such as the "tree" command.
12595
I'm using find with the -exec option in console, on FreeBSD (for example, find . -exec sha1 {} \;).   How do I correctly place (and if needed, escape) redirect and other execution control symbols that could syntactically apply to either of the exec command or the find command, such as > >> | and &amp;, where I might want the symbol to apply to the find or the exec commands on different occasions?  On the same theme, what is the syntax if I want to use tee to view the output of the -exec commands on the console (to watch progress) and also appended to a file (for later use)?   (I know shells vary but hopefully the most used shells such as sh/csh are quite similar?)  Update:  In part, I want to learn if nested find -exec can be done without scripting, as a simple command. Here is the use-case that prompted the questio :   outer find: find all subdirs matching DIR_MATCH_TEXT in DIR1  inner find: for each matching subdir found in the outer loop (call it DIR2), execute the commandfind DIR2 -name "FILE_MATCH_TEXT" -exec sha1 {} \; &gt;&gt; DIR1/DIR2_hashes.txt   The aim being to create a set of files, one for each matching subdir, containing the output of some find -exec action on that subdir.  By this I mean that, if /backups contains /jan2017 and /feb2017, the result will be two files at /backups/jan2017_hashes.txt and /backups/feb2017_hashes.txt, with jan2017_hashes.txt containing the output from sha1 for (say) all .pdf files in /backups/jan2017, and feb2017_hashes.txt containing the output from sha1 for all .pdf files in /backups/feb2017.  From replies so far I gather the outer find would have to use a shell call as the argument to -exec?
45158
How would one run a specific command for each file that was found by using the find command? For the purpose of the question lets say that I would simply like to delete each file found by find.
21921
I have a line as follows   // Testing this   I am trying to use a sed command to replace the // with a /* (sentence or word in between) */ on each line.  so it should look something like this  /* Testing this */   The first part is easy with calling  sed 's#//#/*#'   however, with the second part, I tried this solution    I tried with -e which gave me an error of 'unterminated s command'. Then, I tried with a semi colon to make it one so it was something along the lines of   's#//#/*#;#//#s#/$#*/#'   but all that seems to do is the first part (replacing // with /*)and not the second (putting a */ at the end of the same line).  What am I not doing correctly? Any pointers would be appreciated.
46361
I'm looking for an OS X tool which allows me to search and replace a search string in multiple files. The requirements are the following:   Search and Replace Search by regular expression Search across multiple files Search across nested directory structures Replace expression can use values from the search (Regular Expression) The tool has an easy to use user interface Preferably free   I know that some text editors like BBEdit have this functionality, but I would prefer a lightweight tool that doesn't require me to open the files for editing first.  If there aren't any tools to match these requirements, I would also consider a command line alternative. I know that perl and sed support something like this - a solution that I can define either as a Bash alias or function would be welcome in this case.