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 (...
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 ...
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]/...
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 ...
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 ...
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 ...
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, ...
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.
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, ...
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.
...
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".
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 ...
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 ...
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 ...
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. ...
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"
...
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 ...
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. ...
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, --...
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 ...
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 ...
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,...
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.
...
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 /...
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....
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"/...
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 | ...
...
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 -...
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 ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
recursive × 355find × 74
files × 60
bash × 54
directory × 46
shell-script × 39
grep × 35
shell × 32
rename × 28
cp × 25
command-line × 20
wildcards × 19
rm × 19
linux × 16
wget × 16
file-copy × 13
search × 13
permissions × 12
filenames × 11
ls × 10
symlink × 10
mv × 10
zip × 10
scripting × 9
rsync × 9