As of May 31, 2023, we have updated our Code of Conduct.

New answers tagged

0 votes

Find command very slow in directory with tons of files and subdirectories

I haven't used -empty myself, but it could potentially be costly. Also, without -depth to do a depth-first search you will only find directories at the bottom level and not recursively delete ...
LustreOne's user avatar
  • 1,525
0 votes

Need help in running find command reading the file name from the for loop

If the point is to find the regular files, last modified in the last 10 minutes whose last paths components are any of the strings that make up the lines of the /tmp/fefile, and whose path contains at ...
Stéphane Chazelas's user avatar
1 vote

Need help in running find command reading the file name from the for loop

The main problem with your script fragment is that you are running find multiple times in a loop (once for each filename in /tmp/fefile). This is extremely slow and inefficient because find is an &...
cas's user avatar
  • 75.1k
1 vote
Accepted

How to display file and folder properties from search

Rather than the size of the directory files¹, it looks like you want the cumulative size or maybe disk usage of those directories and all the the files that can be found through their traversal. Then ...
Stéphane Chazelas's user avatar
-1 votes

Apply arithmetic into piped command

Notwithstanding the advice of other answers, in general, you'll need to combine two commands to create a one-liner shell arithmetic filter function you can use in a pipeline: xargs ability to map ...
bishop's user avatar
  • 3,102
0 votes

Need help in running find command reading the file name from the for loop

In the find command, you use a regex. This regex looks through the filenames listed in the fefile file and none of them match this regex. NOTE: it looks in the filenames themselves, not inside the ...
Daniel's user avatar
  • 26
3 votes

How do I make the find command not prefix results with ./?

The ./ in find's output comes from the . argument before the options. Changing the . argument changes the part of the output you're focused on. Given your example, this would produce the output you'...
Sotto Voce's user avatar
  • 2,971
3 votes
Accepted

How do I make the find command not prefix results with ./?

You can modify the find command to remove the ./ prefix from the output by using the GNU-specific (but then again your -regextype is also GNU-specific) -printf predicate instead of -exec. Here's an ...
k.Cyborg's user avatar
  • 447
2 votes

Apply arithmetic into piped command

Well, bash being acceptable #!/bin/bash shopt -s dotglob globstar extglob nullglob oldest=1685518962 newest=1685624474 for file in **/*; do # check for regular file-ness modtime=$(stat -c '%Y' --...
Marcus Müller's user avatar
9 votes

Apply arithmetic into piped command

Use NUL-delimited records (-print0 for find, -0 for xargs) to process file lists, not lines as newline is as valid as any other character (or non-character) in a file path. Since you're already using ...
Stéphane Chazelas's user avatar
4 votes

Apply arithmetic into piped command

This sounds like a job for awk: sudo find /path/to/dir -type f | xargs -d "\n" sudo stat -c "%Y %n" | awk '$1 > 1685518962 && $1 < 1685624474 {print}' You can ...
larsks's user avatar
  • 31.1k
1 vote
Accepted

How to find a file and copy its directory without copying the other files in that same directory?

In zsh, you could do: set -o extendedglob for file ( $SEARCHDIR/**/*.(#i)pdf(ND^/) ) { dest=$OUTPUTFOLDER/${file:t2} mkdir -p -- $dest:h && cp --backup=t -- $file $dest } Where ${file:t2} ...
Stéphane Chazelas's user avatar
0 votes

How to rename multiple files using find

Remove endings *.sample: ls *.sample | cut -d"." -f-2 | xargs -I{} cp {}.sample {}
Anton's user avatar
  • 1
2 votes

if condition with find command in Linux shell scripting

Some comments over your code (assuming it's meant to be in sh syntax): cd /apps/common/bau/base/ You're not checking the exit status of cd, so if it fails, you'll carry on running the rest of the ...
Stéphane Chazelas's user avatar
0 votes

Find files greater than x kB/MB/GB in size, and also show their size

If you use zsh and a ls that supports -h for human readable sizes, you can do something like: ls -lhd -- **/*(.Lm+10) which finds all non-hidden files greater than 10MiB in size and does ls -ldh on ...
pedz's user avatar
  • 173
0 votes

Remove particular word from filename

for file in *.png; do mv "$file" "`echo $file | sed 's/_200x200//'`" done
Nikolay Limunoff's user avatar
0 votes

entr: recognition of new files recursively

I am using this to update an html version of a markdown text: # Preview a comment written in Markdown cpv() { echo ">INFO> Refreshing preview" find | entr -d ...
PabloG's user avatar
  • 1
0 votes

Recursively rename all the files without changing their extensions?

find ~/Desktop/Project/Graphics/ -type f -exec sh -c 'f={}; mv $f ${f%/*}/foo.${f##*.}' \; sh means invoking default shell. you can change it to bash, zsh and so on.
Constantin Hong's user avatar
2 votes

Zero pad file names using find and execdir flag

To zero-pad to length 4 for instance the number at the start of all mp4 and srt file, with zsh: autoload -Uz zmv zmv '(**/)(<->)(*.(mp4|srt))(#q.)' '$1${(l[4][0])2}$3' Or if you have the rename ...
Stéphane Chazelas's user avatar
-1 votes

limit find output AND avoid signal 13

An alternate route for simpler cases could be just a here-string instead of a pipe. E.g - find . -exec stat -c %y {} \; | head -n1 Would see the same issue as above. One easy method to consider - ...
Jay's user avatar
  • 1
2 votes

How do you match zero or one character in the "-name" option of the "find" command?

zsh globs have a **/ recursive globbing operator and glob qualifiers so can do most of what find can do. Zero-or-one of something, aka optional something can be done with (|something) there (nothing ...
Stéphane Chazelas's user avatar
0 votes

How do you match zero or one character in the "-name" option of the "find" command?

Another solution is to use -regex with those find implementations that support it: With GNU find: find . -regextype posix-extended -regex '.*\.i?ly' With BSD find: find -E . -regex '.*\.i?ly' The -...
phuclv's user avatar
  • 1,972
5 votes
Accepted

How do you match zero or one character in the "-name" option of the "find" command?

You can use the -o option, which is an "or" option. The simplest version would be find . -name '*.ly' -o -name '*.ily' This works because -print is the default action. But if the -print was ...
Stephen Harris's user avatar
2 votes

How to run a command only if a specific file has a certain size

Note that some shells have the feature built-in. SHELL=/bin/tcsh * * * * * if (-Z /cache/myfile.csv > 5*1024*1024*1024) echo 'file is > 5GiB' Or with zsh, here using glob qualifiers and an ...
Stéphane Chazelas's user avatar
8 votes
Accepted

How to run a command only if a specific file has a certain size

To use the file size as a precondition you can use stat or find: [ -n "$(find /cache/myfile.csv -prune -size +5G 2>/dev/null)" ] && echo "file is > 5GB" Or if the ...
roaima's user avatar
  • 104k
11 votes

How to run a command only if a specific file has a certain size

If you have GNU stat, you can use its --printf option to get its size. e.g. size=$(stat --printf '%s' /cache/myfile.csv) if [ "$size" -gt 5368709120 ] ; then # 5 GiB = 5 * 1024 * 1024 * ...
cas's user avatar
  • 75.1k
4 votes

How to run a command only if a specific file has a certain size

If you need to treat files in a shell, both version only execute shell's command only if all conditions are met: is a file, is named myfile.csv and is > 5G: find /cache -name 'myfile.csv' -type f -...
Gilles Quénot's user avatar
0 votes

sudo find / -name filename gives invalid argument error

You may have two problems. The first is that you are scanning /proc you should exclude it. I did the same on my rig. And found this error-message on one process. The process was a zombie. Check the ...
ctrl-alt-delor's user avatar
0 votes

sudo find / -name filename gives invalid argument error

There are a number of questions here. Good practice is to ask only one question at a time. errors in output on sudo find / -name filename The /proc filesystem shows you a representation of the running ...
icarus's user avatar
  • 17.1k
1 vote

rsync include exclude is excruciatingly slow

When you compare find and rsync bear in mind that you're not really comparing like for like: find is only scanning the source filetree, rsync is not only scanning the source filetree but also doing a ...
roaima's user avatar
  • 104k
1 vote

bash count files and directory, summary size and EXCLUDE folders that are fuse|sshfs

To skip files on fuse.sshfs FS types, use: find workdir//. -fstype fuse.sshfs -prune -o -type f -print | grep -c // Or use -xdev to not traverse to any other file system (of any type). In any case, ...
Stéphane Chazelas's user avatar

Top 50 recent answers are included