Linked Questions
260 questions linked to/from Why is looping over find's output bad practice?
240 votes
9 answers
328k views
Looping through files with spaces in the names? [duplicate]
I wrote the following script to diff the outputs of two directores with all the same files in them as such: #!/bin/bash for file in `find . -name "*.csv"` do echo "file = $file"; diff $...
4 votes
2 answers
3k views
shellcheck warns about loops over find output even when given the path to begin search [duplicate]
Ubuntu 16.04 #!/bin/bash site="hello" wDir="/home/websites/${site}/httpdocs/" for file in $(find "${wDir}" -name "*.css") do echo "$file"; done exit 0; shellcheck warns me even if I define the ...
3 votes
1 answer
2k views
Handle names with spaces when iterating recursively over files [duplicate]
I am trying to recursively convert all .mkv files in a folder structure with subfolders to .mp4. I got a standard line that works in just the present folder for video in *.mkv; do ffmpeg -i "$...
3 votes
2 answers
2k views
Why I should avoid loops in shells? [duplicate]
The following link recommends against using loops in shells. bash variables in for loop range Why is this? Here is an example loop I just happened to be looking at when I came across that answer: ...
2 votes
3 answers
420 views
How to modify a find function loop to handle backslashes and whitespaces? [duplicate]
Currently, I am trying to parse out all the files inside a directory. I have a find function, but it appears to not be able to parse in file directories with whitespaces into a loop. Here, "$DIR" is ...
1 vote
3 answers
678 views
Fix space in path from find command [duplicate]
I would like to copy many files with the below command. cp `find /Volumes/DATA/ -name "*.app" -depth 1 2> /dev/null` /Volumes/VMWare/img/ But that doesn't work because if it finds a space then it ...
1 vote
2 answers
561 views
Can a pipe be used instead of exec in - find / -name “.txt” -exec cp {} /junk \; [duplicate]
If I want to find any files with .txt in the name, and for every match that is found copy it into the /junk folder, I understand I could use the following: find / -name ".txt" -exec cp {} /junk \; ...
0 votes
1 answer
773 views
remove spaces in filenames in find output [duplicate]
I want to list the size of directories under a given root (using du -hs) . The issue is that where the directory names have spaces the du command is getting each word of the name as a whole directory ...
1 vote
3 answers
139 views
A for-do loop not working for filenames containing a space [duplicate]
for X in $(find ~/Documents/ -type f);do date -r "${X}";done The above line works fine when the filenames in "/Documents/" contain no space. However, if the filenames contain a ...
0 votes
0 answers
802 views
Shell scripting with brackets/parenthesis in folder names [duplicate]
I have a music library that I am trying to restore from backup. The original source library was on a QNAP NAS and I copied it over to a CentOS system without any problems. Now that I am trying to copy ...
0 votes
1 answer
130 views
Problem using find with filenames containing spaces [duplicate]
I'm trying to run join a bunch of files using the find command. I have determined that filenames containing spaces are giving me a problem. The following comand works for files that do not contain ...
1 vote
1 answer
95 views
How to match files that have spaces in their file naming format [duplicate]
I have a file test.txt which contains file names like below where some file names will have spaces and some will not. Mon - Tue corrected item.csv Sat -Sun incorrect item.csv Wed_THU_corrected_item....
0 votes
0 answers
104 views
Working with strange filenames [duplicate]
How can the script below be made to always work no matter the filenames returned by find ? #!/bin/sh cmds_should_always_work() { echo "\n\n*********************************" $1 stat --...
0 votes
1 answer
74 views
How to loop over subdirectories and perform action while ignoring spaces in file names [duplicate]
I have the below folder structure in Linux env: |-- Fixed_Zip | |-- ipython_notebooks | | |-- notebook editor for compute_0anXhGEj.ipynb | | |-- notebook editor for compute_aucScores.ipynb | ...
0 votes
0 answers
19 views
Loop through files from current directory and subdirectories [duplicate]
I'm trying to loop through each files from current directory and subdirectories. But the problem is file names with blank-space are getting treated as separate files. How do I prevent that. I want to ...
540 votes
14 answers
932k views
How do I loop through only directories in bash?
I have a folder with some directories and some files (some are hidden, beginning with dot). for d in *; do echo $d done will loop through all files and directories, but I want to loop only through ...
787 votes
6 answers
1.3m views
Problem running find: missing argument to `-exec'
I'd like to find the files in the current directory that contain the text "chrome". $ find . -exec grep chrome find: missing argument to `-exec' What am I doing wrong?
386 votes
14 answers
733k views
How to list all files ordered by size
I would like to list all files in the order of big to small in size and the files could be present anywhere in a certain folder.
282 votes
10 answers
99k views
Why *not* parse `ls` (and what to do instead)?
I consistently see answers quoting this link stating definitively "Don't parse ls!" This bothers me for a couple of reasons: It seems the information in that link has been accepted wholesale with ...
378 votes
6 answers
421k views
Why does my shell script choke on whitespace or other special characters?
… 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 ...
288 votes
5 answers
79k views
Why is using a shell loop to process text considered bad practice?
Is using a while loop to process text generally considered bad practice in POSIX shells? As Stéphane Chazelas pointed out, some of the reasons for not using shell loop are conceptual, reliability, ...
169 votes
6 answers
423k views
How to merge all (text) files in a directory into one?
I've got 14 files all being parts of one text. I'd like to merge them into one. How to do that?
145 votes
8 answers
68k views
When is xargs needed?
The xargs command always confuses me. Is there a general rule for it? Consider the two examples below: $ \ls | grep Cases | less prints the files that match 'Cases', but changing the command to ...
124 votes
6 answers
319k views
How to integrate mv command after find command?
I am searching for files which name which contain AAA within their path using following command: find path_A -name "*AAA*" Given the output showed by the above command, I want to move those files ...
149 votes
4 answers
199k views
How do I pass a list of files to grep
I am using find and getting a list of files I want to grep through. How do I pipe that list to grep?
196 votes
2 answers
210k views
Understanding the -exec option of `find`
I find myself constantly looking up the syntax of find . -name "FILENAME" -exec rm {} \; mainly because I don't see how exactly the -exec part works. What is the meaning of the braces, the backslash ...
55 votes
16 answers
35k views
How can I delete a file which filename has non-printing characters
I somehow managed to create a file that doesn't seem to have a filename. I found some information regarding how to get more details of the file in the following thread. However, I tried some of the ...
98 votes
5 answers
144k views
How do I recursively delete directories with wildcard?
I am working through SSH on a WD My Book World Edition. Basically I would like to start at a particular directory level, and recursively remove all sub-directories matching .Apple*. How would I go ...
37 votes
14 answers
63k views
Preserve directory structure when moving files using find
I have created the following script that move old days files as defined from source directory to destination directory. It is working perfectly. #!/bin/bash echo "Enter Your Source Directory" read ...
49 votes
4 answers
27k views
Strange errors when using ffmpeg in a loop
I have a bash script looping through the results of a find and performing an ffmpeg encoding of some FLV files. Whilst the script is running the ffmpeg output seems to be interupted and is outputting ...
28 votes
8 answers
60k views
ls content of a directory ignoring symlinks
I have a directory in which I would like to list all the content (files and sub directories) without showing the symbolic links. I am using GNU utilities on Linux. The ls version is 8.13. Example: ...
36 votes
7 answers
130k views
How can I use bash's if test and find commands together?
I have a directory with crash logs, and I'd like to use a conditional statement in a bash script based on a find command. The log files are stored in this format: /var/log/crashes/app-2012-08-28.log ...
32 votes
3 answers
72k views
Copying files with certain extensions with scp
I want to copy over .jpg and .png files with scp, but there files with different extensions in the same folder I'm copying from. I am doing the following: scp [email protected]:/folder/*.{jpg,...
23 votes
4 answers
31k views
Convert every PDF in the current directory to PNG
I want to write a Bash script to convert every .pdf file in the current directory into a .png file. For example: $ls . a.pdf b.pdf $./pdf2png.sh Converting pdfs to pngs a.pdf -> a.png b.pdf -> ...
23 votes
5 answers
55k views
How do I perform an action on all files with a specific extension in subfolders in an elegant way?
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" ...
22 votes
3 answers
50k views
Why doesn't "ls *.e*" work as argument to "find -execdir"?
I tried finding some files (*.e*) that are in the same directory as another file (md.tpr). I needed to list them (for further processing) using the following: find . -name md.tpr -execdir ls *.e* \; ...
15 votes
5 answers
51k views
How to copy and add prefix to file names in one step?
I want to copy and rename multiple c source files in a directory. I can copy like this: $ cp *.c $OTHERDIR But I want to give a prefix to all the file names: file.c --> old#file.c How can I do ...
19 votes
7 answers
24k views
How to find all git repositories within given folders (fast)
Naive approach is find dir1 dir2 dir3 -type d -name .git | xargs -I {} dirname {} , but it's too slow for me, because I have a lot deep folder structures inside git repositories (at least I think that ...
10 votes
4 answers
38k views
Grep word within a file then copy the file
I have a collection of files ( *.zip, *.txt, *.tar.gz, *.doc, ...etc ). These files reside within a path. I want to find all the files ( *.txt), then copy, only, the text files that contain specific ...
14 votes
4 answers
41k views
How do I use command substitution in find -exec?
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 use $(basename {} .txt) in the -exec option, ...
8 votes
8 answers
7k views
How to count total number of lines of all .txt files?
I'm trying to figure out how to get total number of lines from all .txt files. I think the problem is on the line 6 -> let $((total = total + count )). Anybody knows what's to correct form of this? ...
14 votes
4 answers
54k views
Delete all directory that begin with a particular string
What command do I have to use to delete all directories that begin with graphene-80 under the directory /tmp? What can I add to the rm command as option?
14 votes
4 answers
6k views
Directories with two or more files
I want to find a subdirectory of the current directory, which (that is the subdirectory) contains 2 or more regular files. I am not interested in directories containing less than 2 files, neither in ...
12 votes
4 answers
4k views
bash: whitespace-safe procedural use of find into select
Given these file names: $ ls -1 file file name otherfile bash itself does perfectly fine with embedded whitespace: $ for file in *; do echo "$file"; done file file name otherfile $ select file in *; ...
14 votes
2 answers
23k views
if command in find -exec
I was just trying to list all directories and files under current directory and also write if they are file or directory with the following command: find -exec echo `echo {} : ;if [ -f {} ]; then ...
5 votes
6 answers
1k views
How to find files that don’t have a suffixed version?
I have a few million .jpg files and I want to generate a .jpg.webp version for each one (foo.jpg -> foo.jpg.webp). For this, I need to find all files ending in .jpg for which there’s no .jpg.webp ...
4 votes
8 answers
6k views
How to name a file in the deepest level of a directory tree
How do I name one random file in the deepest level of a directory tree using basic Bash commands and script? I was searching for a way to do it as an one-liner and without functions. Assuming also ...
5 votes
4 answers
16k views
Add file extension to files that have no extension
I have hundreds of files in various different subdirectories. Some of them have the correct file extension, but some of them don't. I want to rename all files that don't have a file extension and ...
9 votes
3 answers
14k views
Remove many many many files from a folder
I have a folder with 137795 files in it. I need to delete all of them. When I run rm * I get -bash: /bin/rm: Argument list too long. How do I get past this error?
5 votes
7 answers
10k views
Running a command multiple times with arguments (filenames) from a file?
I have a file with a long list of filenames (with full paths). I've also got a program I'd like to run multiple times, using one and one filename from this list as argument. The program I'd like to ...