36
votes
Accepted
What is the practical purpose of "./" in front of relative file paths (in the output from "find")?
This behaviour comes from find, and is specified by POSIX:
Each path operand shall be evaluated unaltered as it was provided, including all trailing <slash> characters; all pathnames for other ...
11
votes
What is the practical purpose of "./" in front of relative file paths (in the output from "find")?
There is no practical use when we talk about ordinary files. But when we talk about executable files it make sense. When you execute (and file1 is located in current directory):
file1
shell search ...
10
votes
Accepted
mv: A and B are the same file
Your find command moves files into the directory called MOV in the current directory.
The way you have written the command, it will also look for files in the MOV directory, and when it finds matching ...
5
votes
What is the practical purpose of "./" in front of relative file paths (in the output from "find")?
Many commands in Linux receive - as a special file denoting stdin, therefore to indicate a file whose name is really - you must use ./-. See
Usage of dash (-) in place of a filename
What does dash &...
5
votes
Accepted
find the directory using pattern and delete the content, not the directory itself
You can match against the path instead:
find . -path "*/results_*/*" -delete
or, if your find doesn’t support -delete,
find . -path "*/results_*/*" -exec rm -rf {} \; -prune
This ...
5
votes
exclude directory from find command
find . -path ./mnt -prune -o -name example.txt -print
Which is short for:
find . '(' -path ./mnt -a -prune ')' -o '(' -name example.txt -a -print ')'
AND (-a, implied when omitted) has higher ...
4
votes
find all files with either extension .ts or .tsx in Unix?
You can do:
find . \( -name '*.tsx' -o -name '*.ts' \) -print0 |
xargs -0 sed -i "" "s/SomeThing/SOME_THING/g"
Or, simpler and more portable (and avoids a sed error if not ...
4
votes
Accepted
Is it possible to chain two consecutive find commands?
Here, you can do it with one find expression and even avoid the -mindepth / -maxdepth GNUisms with:
find / ! -path / ! -path '/[[:upper:]]*' -prune -o -name .git -type d -print
More generally, you ...
4
votes
Is it possible to chain two consecutive find commands?
Since the first find is only searching a single level, you can replace it with a simple shell wildcard pattern: /[[:upper:]]*. Then use that as the places for find to search:
find /[[:upper:]]* -type ...
4
votes
Why basename don't work here
Your code will fail for any filename that contains a space (e.g this one.txt) or on any system where the user name or group name contains spaces (a system joined to Active Directory, for example).
The ...
4
votes
linux find command + delete all snapshot files except last 5 ordered by date
In zsh:
rm -f /zookeeper/version-2/snapshort.*(N.om[6,-1])
Where om orders by modification time (newest first like ls -t does), . restricts to regular files, N enables Nullglob, [6,-1] selects the ...
4
votes
Accepted
linux find command + delete all snapshot files except last 5 ordered by date
Remove directly zookeeper snapshots may have unpredictable results for the hadoop cluster. There is specific command for this purpose:
zkCleanup.sh -n 5
3
votes
Executing find command with a file having directory lists
GNU find can read the starting points iteratively from a file with the -files0-from option, which specifies a file containing NUL-delimited set of paths. To process only those specified directories ...
3
votes
Escape curly brace in sed when using find
There is no way to make find not replace {} in the argument to -exec. Instead, modify the {} in the sed expression so that it's no longer exactly {}. For example, you may just escape the }:
find . -...
1
vote
How to rename files recursively with a specific extension to remove unwanted characters in audio files
There must be some other issue at work, because ffmpeg is perfectly able to read filenames containing single quotes:
mkdir '2005-09-30 - The Poison (Deluxe Edition) [US - 88697-09021-2 - 2007]'
cp -p {...
1
vote
How to rename files recursively with a specific extension to remove unwanted characters in audio files
First of all we write a shell script that move $1 to $1 without (') and we call it special_move.sh. Do not forget to give the file execution permission.
#!/bin/bash
orgFile=$1
destFile=${orgFile//\'/}...
1
vote
Accepted
Detect leaf directories in Perl
You could do something like:
perl -MFile::Find -le '
find(sub {
if (-d _) {
undef $leaves{$File::Find::name};
delete $leaves{$File::Find::dir};
}
}, &...
1
vote
replacing part of find output in a readarray command
With zsh instead of bash:
() {print -rC1 -- $tdir/$^@} $fdir/*.mp3(Noe['REPLY=$RANDOM']:t)
Or assuming neither $fdir nor $tdir contain : characters:
print -rC1 -- $fdir/*.mp3(Noe['REPLY=$RANDOM']:s:$...
1
vote
Accepted
replacing part of find output in a readarray command
As @icarus has pointed out, the -exec option doesn't make sense here - you want to apply your sed substitution to the null-delimited filenames, using a sed pattern delimiter that is unlikely to be ...
1
vote
find all files with either extension .ts or .tsx in Unix?
There is no need to run find in this case. If using the bash, shell (at least version 4.0, preferably 5.0 or above which has fixed a few bugs in the globstar implementation), you can use globbing ...
1
vote
find all files with either extension .ts or .tsx in Unix?
You can use a combined filter with -regex instead of -name. Given the following files
src/
src/subdir-2
src/subdir-2/not-interesting.tsn
src/subdir-2/bar.ts
src/subdir-2/bar.tsx
src/subdir-2/not-...
1
vote
Running command on file(s) in the result of a `find`
Skipping the cat command fixes your issue:
find . -name myfile -exec sed -i -e '1 c my new line' -e '2d' {} \;
The {} argument in exec is the filename, so it runs the sed -i command in place for each ...
1
vote
Accepted
Bulk replace multiple variables in multiple files at once
Split the problem into two parts:
Iterate across the files
Apply the substitution to a file
If you are in the EXAMPLE directory you can loop across all the files like this
for file in *.txt
do
...
1
vote
Fastest way to recursively list all the files in a directory
This cheats and assumes you have an updated location database (like plocate), but it should be the fastest simply because you're parsing through a cache of the directory contents.
locate "$PWD/*&...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
find × 3319bash × 595
linux × 414
shell × 352
shell-script × 348
grep × 346
files × 338
command-line × 195
xargs × 165
directory × 146
regular-expression × 128
sed × 127
ls × 125
rm × 120
filenames × 109
rename × 106
wildcards × 89
scripting × 86
mv × 84
awk × 81
recursive × 72
macos × 71
permissions × 69
cp × 65
pipe × 62