Skip to main content
40 votes
Accepted

cp behaves weirdly when . (dot) or .. (dot dot) are the source directory

The behaviour is a logical result of the documented algorithm for cp -R. See POSIX, step 2f: The files in the directory source_file shall be copied to the directory dest_file, taking the four steps (...
Stephen Kitt's user avatar
27 votes

How to recursively find the amount stored in directory?

This will give you a list of sizes from current directory, including folders(recursive) and files. $ du -hs * 7.5M Applications 9.7M Desktop 85M Documents 16K Downloads 12G Google ...
Simon Liu's user avatar
  • 371
19 votes
Accepted

How to chmod only on subdirectories?

You can use wildcards on the top level directory. chmod 774 d*/workspace Or to make it more specific you can also limit the wildcard, for example to d followed by a single digit. chmod 774 d[0-9]/...
Thomas's user avatar
  • 6,632
17 votes
Accepted

How do I recursively run "chgrp" without changing the group if it matches a specific group?

Use find to exclude anything owned by group docker; starting from the target home directory: find . ! -group docker -exec chgrp newgroup {} + replacing newgroup as appropriate. Alternatively, look ...
Stephen Kitt's user avatar
15 votes

How do I count all the files recursively through directories

du --inodes (GNU version only) du --inodes --inodes List inode usage information instead of block usage. This option is useful for finding directories which contain many files, and therefore eat up ...
Sridhar Sarnobat's user avatar
12 votes

Flattening a nested directory

tar and zip both have the ability to incorporate and then strip away a directory structure, so I was able to quickly flatten a nested directory with tar -cvf all.tar * followed by moving all.tar to ...
John's user avatar
  • 121
12 votes
Accepted

Handle names with spaces when iterating recursively over files

Your error lies in using find in a command substitution. A command substitution always results in a single string. If you leave the substitution unquoted, that string will undergo splitting on spaces, ...
Kusalananda's user avatar
  • 356k
11 votes
Accepted

How do I make this rsync command non-recursive?

You can add option --exclude='*/' to your rsync options to prevent syncing of directories.
Freddy's user avatar
  • 26.3k
10 votes

Flattening a nested directory

Expanding on the popular answer for this question, since I had a use-case for flattening a directory containing files of the same name. dir1/ ├── dir2 │ └── file └── dir3 └── file In this case, ...
Yann Eves's user avatar
  • 201
9 votes
Accepted

How to remove all occurrences of .DS_Store in a folder

find top-folder -type f -name '.DS_Store' -exec rm -f {} + or, more simply, find top-folder -type f -name '.DS_Store' -delete where top-folder is the path to the top folder you'd like to look under. ...
Kusalananda's user avatar
  • 356k
8 votes

How do I count all the files recursively through directories

If you have ncdu installed (a must-have when you want to do some cleanup), simply type c to "Toggle display of child item counts". And C to "Sort by items".
Jean-Didier's user avatar
8 votes

How can I use rename to recursively rename everyting to uppercase

I'd like to direct anyone who's still being linked to this answer to the excellent answer Guiles Quernot gave to this question which doesn't require find. The resulting command would be: shopt -s ...
Marcelo Lacerda's user avatar
8 votes

How do I make this rsync command non-recursive?

Extract from man rsync -a, --archive archive mode; equals -rlptgoD (no -H,-A,-X) -a implies -r so you can : remove `-a --no-perms --no-o --no-g ` and replace by `-ltD` or add ...
EchoMike444's user avatar
  • 3,225
7 votes

How to recursively find the amount stored in directory?

I like the following approach: du -schx .[!.]* * | sort -h where: s: display only a total for each argument c: produce a grand total h: print sizes in a human-readable format x: skip directories ...
Eduardo Baitello's user avatar
7 votes
Accepted

Accidentally created symbolic link to a folder in that folder

When you have a symbolic link to a directory, if you add a trailing slash to the name then you get the directory itself, not the symlink. As a result: rm link/ will try to remove the directory. ...
Andy Dalton's user avatar
  • 14.7k
7 votes

Recursively iterate through all subdirectories, If a file with a specific extension exists then run a command in that folder once

You're re-inventing find. Try something like this (using GNU findutils and GNU sort): find /target -iname '*.xyz' -printf '%h\000' | sort -z -u | xargs -0 -r -I {} sh -c "cd {} ; yourcommandhere" ...
cas's user avatar
  • 83.9k
7 votes

why doesn't cp automatically copy directories recursively?

Like many other details of Unix commands, this is probably historical. In Unix, cp never supported recursive copies; instead, a specific recursive cp, rcp, was added in V8, replaced later by reccp. cp ...
Stephen Kitt's user avatar
6 votes

Using sftp to Transfer a Directory?

If you question is actually 'how do I use sftp to transfer a directory', then sftp -r user@server But if you transferring a directory, I might suggest two better options, depending on your needs. ...
Dan Williams's user avatar
6 votes

How to copy a directory recursively using hardlinks for each file

rsync -av --link-dest="$PWD/dirA" dirA/ dirB If you happen to have rsync already installed this one is a quick simple command. To cope with symlinks you may want to choose among --links, --...
Adan Cortes's user avatar
6 votes

How to chmod only on subdirectories?

The chmod command has a nice shortcut for setting the executable bit only on directories, like so: chmod a+X * This is very handy to make a whole directory tree readable by anyone, but not setting ...
G. Sliepen's user avatar
6 votes

Sort and remove duplicates in place and recursively

That can be just: find . -type f -size +1c -exec sort -uo {} {} ';' (here skipping the files that are less than 2 byte large as you need at least 3 bytes to make two different lines, or possibly 2 ...
Stéphane Chazelas's user avatar
6 votes

How do I recursively run "chgrp" without changing the group if it matches a specific group?

How do I recursively run (…) so that every single file/subdirectory whose (…) is (…) The generic answer to this is either find or zsh globs. Use one of these to enumerate the files you want to affect,...
Gilles 'SO- stop being evil''s user avatar
5 votes

Move all files with a certain extension from multiple subdirectories into one directory

This one is safe when moving data and error free which supported most of all distro regardless versions. This command will scan subdirectories and then move or copy to your new destination directory. ...
Seandex's user avatar
  • 211
5 votes
Accepted

Fast string replace in recursive directories

You'd only want to use the: find . -name '*.txt' -exec cmd {} \; form for those cmds that can only take one argument. That's not the case of grep. With grep: find . -name '*.txt' -exec grep foo /...
Stéphane Chazelas's user avatar
5 votes

Recursively rename all the files without changing their extensions?

It's very similar with find...-exec: invoke a shell so that you can use parameter expansion, extract the PARENT directory and the EXTENSION so that you can construct the new filename as PARENT/NAME....
don_crissti's user avatar
  • 85.6k
5 votes
Accepted

cp -a doesn't work to copy all files inside a directory

Scenarios: Directory a exists but b doesn't`: cp -a "$HOME"/a "$HOME"/b will copy the entire directory a to a new directory b Directories a and b exist: cp -a "$HOME"/...
schrodingerscatcuriosity's user avatar
5 votes

How might one except hidden '.' files and directories from a script that renames them?

Without looking too much at the other commands in the pipeline, you can make find avoid entering any directory that has a hidden name: find . ! -path . -name '.*' -prune -o -name '* *' -print0 | ... ...
Kusalananda's user avatar
  • 356k
5 votes
Accepted

How to recursively delete the contents of all "node_modules" directories (or any dir), starting from current directory, leaving an empty folder?

The following will delete all files and directories within a path matching node_modules: find . -path '*/node_modules/*' -delete If you would like to check what will be deleted first, then omit the -...
Snake's user avatar
  • 1,760
5 votes
Accepted

BASH recursive rename of js files to jsx

Try this. $ find . -name '*.js' -exec echo mv {} {}x \; mv ./a/a.js ./a/a.jsx mv ./a/z/z.js ./a/z/z.jsx mv ./b/b.js ./b/b.jsx $ Once you're happy with how the proposed commands look, just remove the ...
steve's user avatar
  • 22.3k

Only top scored, non community-wiki answers of a minimum length are eligible