I want to grep across 500 files in a Maildir directory. I issued the command  grep MyPattern *   I got the error message:  bash: /usr/bin/grep: Argument list too long   So I stored the list of files in a file MyFiles, and issued the following  for i in $(`cat MyFiles`); do echo $i; done   Before I did a grep, I wanted to do an echo just as a check. But this gave the following error  bash: 1434361691.M617282P6399V0000000000000808I00000000000E16C1_23.ananda-linux,S=10055:2,S: command not found   where that 1434... thing is the first file in the directory.  So back to the original question. How do I grep across all these files in the mailbox. And I have larger mailboxes containing 50000 or more emails.
121733
I have directory with cca 26 000 files and I need to grep in all these files. Problem is, that I need it as fast as possible, so it's not ideal to make script where grep will take name of one file from find command and write matches to file. Before "arguments list too long" issue it took cca 2 minutes to grep in all this files. Any ideas how to do it? edit: there is a script that is making new files all the time, so it's not possible to put all files to different dirs.
17335
file_A (~500MB, 1.6M lines) consists of all equal length search terms, 1 per line, not sorted.  file_B consists of all equal length text lines, 1 per line, not sorted  I've been able to run "grep -F -f file_A file_B >> output.txt" with any size file_B without problem on a box with 52GB ram. Problem is I'm now limited to 4GB ram and thus the size of file_A is now too large for this to run without exhausting available memory.  Short of manually chopping up file_A into smaller bites, is there any easy way to script this to grep for first 1000 lines of file_A, then when thats finished to automatically grep for lines 1001-2000, ect. until I've gone through all of file_A?
100532
The runtime arguments are as follows: $1 is the path to the file containing the list of files $2 is the path to the directory containing the files What I want to do is check that each file listed in $1 exists in the $2 directory  I'm thinking something like:  for f in 'cat $1' do if (FILEEXISTSIN$2DIRECTORY) then echo '$f exists in $2' else echo '$f is missing in $2' sleep 5 exit fi done   As you can see, I want it so that if any of the files listed in $1 don't exist in the directory $2, the script states this then closes. The only part I can't get my head around is the (FILEEXISTSIN$2DIRECTORY) part. I know that you can do [ -e $f ] but I don't know how you can make sure its checking that it exists in the $2 directory.
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.
47502
I remembered bash can do the following:  Suppose there are 3 files: a,b,c in the current directory, when I input:  $&gt; somecommand *   and then press a key or key combinations or a key sequence, the wildcard * online expanded as:  $&gt; somecommand a b c   but I cannot remember and cannot google out the key sequence! help!
9006
Grep works when I use is with ls | but when I try the command by itself, it just gives me a blinking cursor that never goes away on the next line (as if it's running a command).  For example, I tried:  ls | grep zip   while in the bin directory and got a list of files, but plain grep zip in the same directory gives me the problem stated above.
60609
My data is like:   First_file.txt  Start abcd efgh hijk lmn  Second_File.txt  opq rst uvw xyz END      I want to grep all the line between patterns START &amp; END even END pattern in next file  Output should be like:  abcd efgh hijk lmn opq rst uvw xyz
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
113848
I got a bunch of files in some directory (along with many other files) that I want to move.   Luckily, all the files I want to move contain a certain identifier in their names, so I can ls | grep IDENTIFIER to get the exact list of files to move.   But, how can I execute mv file /path/to/dest/folder/ at once, and not one by one (there's a lot of files to move)?
84966
I want to execute a command for an each file that matches a pattern. But the command that seems to be right, doesn't work for me, I have no idea why.  $ find . -type f -name '*.c' -or -name '*.h' -or -name '*.cpp' -exec ls {} \; $ ls script.sh  test.c  test.h
84769
I have a folder with 36,348 files gz files. I want to unzip all of them.  Running:   gunzip ./*   results in   -bash: /usr/bin/gunzip: Argument list too long   What's the easiest way to get around this?
115449
Stupidly, I had been using a condition like this as part of a script:  if [ $(ls FOO* 2&gt; /dev/null) ] # if files named "FOO*" were downloaded  then     echo "Files found"      # ... process and email results else     echo "Not found"     # ... email warning that no files were found (against expectations) fi   That works for zero and one files named FOO*, but fails if there are more than one. From logs I found several different error messages stemming from this:  [: FOO_20131107_082920: unary operator expected [: FOO_20131108_070203: binary operator expected [: too many arguments   My question is: what is the correct way to check, in a Bash if condition, whether one or more files whose name begins with FOO exist?  GNU bash, version 4.2.25(1)-release (x86_64-pc-linux-gnu)
90095
I'm trying to get my head around finding files in terminal at the moment. I've found a lot of answers about how to find files to a specific pattern, such as files ending in a certain extension, or from a certain timeframe, etc.  What I've been trying to figure out, though, is how to find and copy multiple arbitrary files, like a list of specific files.  Say, for example, I have the following folders and files  /Documents/Folder_1/ with contents:   file1.txt file2.txt file3.txt file4.txt   /Documents/Folder_2/ with contents:   file5.txt file6.txt file7.txt   /Documents/Folder_3/ with contents:   file8.txt, file9.txt   and what I want to do is to make a new folder called Folder_4 and copy file2.txt, file5.txt and file9.txt to that new folder.  Is there any way to do this through terminal?  What I've tried to do is to make a text list of the file names and call it something like list.txt, e.g.  file2.txt file5.txt file9.txt   and loading that text file into the Find command, for example like this:  find /home/usr/Documents -name $cat list.txt   with the thinking that I would then feed the output to either a text list or a copy command, but this doesn't seem to work.  I'm wondering if there is any way to find multiple arbitrary files through the Find command, or am I thinking about this in the wrong way?  EDIT: Just wanted to say thanks for the answers and summarise the way that I ended up approaching this problem. I especially appreciate that someone explained to me that the find command is not designed to accept more than one input without the -o flag, it made me feel a bit less stupid for not being able to figure it out!  What I ended up doing was to make a little shell script and ask for user input and use an array and a for loop to iterate through the list.  Basically the script that I ended up with is more or less like this:  #!/bin/bash  #take user input echo "Please enter list of filenames to search" read list echo "Please enter the folder path to search" read path echo "Please enter the destination folder path" read destination  #use inputted list as input for array, and then use a for loop to find each item on list one by one array=($list) for i in "${array[@]}" do     find "$path" -iname "i" -exec cp '{}' "$destination" \; done
47341
I'm having issues with a script i'm writing.  I'm trying to monitor free disk space, and to have an email generated for each filesystem over a certain threshold.  When using this script with 'echo' instead of 'mail', the output looks correct to my terminal.  When i incorporate mail, only the critical warning is sent, and the body of the email includes the other filesystems.  I'm trying to send individual emails for each filesystem.  I'm not looking for an answer, but maybe a certain place to focus on to try and determine what my issue is.     #!/bin/bash     #The following script will check filesystems (local/nfs only)     #and notify via email current user if over certain threshold.      #Thresholds for script to call     CRITICAL=90     ALERT=70      #Gets local/nfs disk info, greps out header/tmp filesystems and awks column 1 and 5     #while read loop is enabled to parse through each line     df -l | grep -vE '(^Filesystem)' | awk '{ print ($5  " " $1)}' | while read output;     do         #updates variables to reads current step         usage=$( echo $output | awk '{print $1}' | cut -d'%' -f1 )         part=$( echo $output | awk '{print $2}' )          #if percentage matches alert or critical, email current user         if [ $usage -ge $CRITICAL ]; then             mail -s "Critical Warning: Filesystem $part is at $usage% of capacity." $USER         elif [ $usage -ge $ALERT ]; then             mail -s "Warning: Filesystem $part is at $usage% of capacity." $USER         fi     done
103460
I have a text file I'm outputting to a variable in my shell script.  I only need the first 50 characters however.  I've tried using cat ${filename} cut -c1-50 but I'm getting far more than the first 50 characters?  That may be due to cut looking for lines (not 100% sure), while this text file could be one long string-- it really depends.    Is there a utility out there I can pipe into to get the first X characters from a cat command?
80255
If parsing the output of ls is dangerous because it can break on some funky characters (spaces, \n, ... ), what's the best way to know the number of files in a directory?  I usualy rely on find to avoid this parsing, but similarly, find mydir | wc -l will break for the same reasons.  I'm working on Solaris right now, but I'm looking for a answer as portable across different unices and different shells as possible.
59954
I have around 100,000 files in a directory. As you can imagine, that's not an ideal situation, and I'd like to split the directory up into subdirectories. But the filenames aren't such where I can put them into subdirectories based on their names or anything, so I'd like to create, say, 10 directories and put about 1000 files in each. Is there an easy bash command or loop that would do this?