Questions tagged [find]
For questions pertaining to find, a command-line utility to search for files in a directory hierarchy. Use this tag for questions about find itself or questions about issues arising from using the find command-line utility.
3,519
questions
0
votes
2
answers
979
views
How to copy files and directories without copying directory contents?
I have a find command that selects some directories and files to be copied to a different location (with --parents). What I'd like is for the directories that are output by the find command to be ...
2
votes
1
answer
2k
views
Append path to filename
I have some pictures called test1.jpg in many subfolders. I want to essentially append the file's path to its name so there's no confusion.
sample path:
/tmp/xxx2016/folder123/folder2/folderxxx/...
1
vote
1
answer
99
views
Why does `find` sometimes find my file, sometimes not [duplicate]
Here is my directory tree (not showing all dirs, files, just the essential):
a_root_dir/ (directory)
a_root_dir/dynamo/local/run.sh
a_root_dir/dynamo/local/run_local.sh
Now when I do
> cd ...
2
votes
5
answers
3k
views
Find all [filename].mp4 and rename [filename].audio [duplicate]
So I have a script that adds 2 films together using the audio from the $1.audio file. What I would like to do is rename any file in the directory with:
*.mp4
To:
*.audio
Keeping original file ...
4
votes
1
answer
1k
views
Increment counter in find -exec
I have a script that runs a number of processes like this
./myproc1 --out=1.xml
./myotherproc --out=2.xml
./mythirdproc --out=3.xml
./myfourthproc --out=4.xml
# ... some more files
I want to ...
23
votes
5
answers
2k
views
Why doesn't find . -delete delete current directory?
I would expect
find . -delete
to delete the current directory, but it doesn't. Why not?
0
votes
2
answers
63
views
How to use find with already written script?
I know how to find last modified files, but how do I use it with my already written script. In folder I have hundreds of files xyz_<yyyymm>, now I use $ find /home/directory/TEST/ -type f -mmin -...
2
votes
2
answers
4k
views
Find files with extensions given in arguments using regex
Let's say a have a following command
search /home/user proc .h .c .txt ...
I am building a script with find command to get all files that start with given name and end with one of given extensions.
...
5
votes
2
answers
15k
views
Replace string in multiple files using find and sed
So I've arrived at the conclusion that for recursively replacing all instances of a string in directory (only for .java extensions files) I need to use
find . -type f -name "*.java" -exec sed -i 's/...
2
votes
2
answers
176
views
count lines matching string in each subdirectory and their subdirectories
In each subdirectory of my current directory, I would like to print the number of line-matches against a string for all files in the subdirectory and its subdirectories.
For example, if I have
cat /...
1
vote
1
answer
63
views
find matching file and change dirname path
I need to find a very efficient way of moving a file of matching -mtime from one directory tree to another directory, maintaining the same subdirectory path where it doesn't exist yet.
eg. move /dirA/...
1
vote
1
answer
1k
views
Find and remove directory from bash
#!/bin/bash
# error handling
function error_handler() {
echo "Error occurred in script at line: ${1}"
echo "Line exited with status: ${2}"
}
trap 'error_handler ${LINENO} $?' ERR
set -o errexit
...
3
votes
4
answers
814
views
How to move a file recursively based on size and keep structure
I want to move files bigger than "300Mb" from one directory tree where each file is located in subfolders
Example: I have a directory structure:
dirA/
dirA/file1
dirA/x/
dirA/x/file2
dirA/y/
dirA/y/...
6
votes
1
answer
2k
views
find -exec mv stops after first exec
While researching for this question: Find and move directories based on file type and date, I stumbled upon a problem of my own.
First I created a bunch of directories with files in them:
seq 10 | ...
-2
votes
1
answer
624
views
How do I print all words begining with a specific letter in a text
How do I print all words begining with a specific letter in a text? and all words of 4 characters?
thanks
287
votes
2
answers
249k
views
how can I recursively delete empty directories in my home directory? [duplicate]
Possible Duplicate:
How to remove all empty directories in a subtree?
I create directories very often, scattered over my home directory, and I find it very hard to locate and delete them.
I want ...
0
votes
3
answers
12k
views
Search multiple directories and output file names
I have a list of 7 directories. Every day they get a file. I would run one command that would check all the directories at once which provides the output of the file names.
Normally I run ls /...
87
votes
4
answers
308k
views
Recursive grep vs find / -type f -exec grep {} \; Which is more efficient/faster?
Which is more efficient for finding which files in an entire filesystem contain a string: recursive grep or find with grep in an exec statement? I assume find would be more efficient because you can ...
1
vote
2
answers
2k
views
find matching files in one directory and sub directories
I have used Percona XtraBackup to create a compressed backup, but now I need to decompress the files. The instructions say I should run:
for bf in `find . -iname "*\.qp"`; do qpress -d $bf $(dirname $...
4
votes
2
answers
4k
views
Filter files generated by `find` by parsed output of `file` command
I'm writing a quick tool to inspect the contents of a node.js node_modules folder or python virtualenv for native dependencies. As a quick first approximation to this I wrote the following command.
...
-1
votes
4
answers
604
views
How to pass a file as an argument in linux and get the list of all files in it from file system?
Lets say I have a file by name searchfiles.txt with file names in it:
2345098.txt
2345099.txt
2345100.txt
and so on (100 file entries)
Now I want to list out or display all the file names that ...
9
votes
1
answer
4k
views
substring removal in find -exec
When using
$ find . -name string~ -exec cp {} FOO \;
is there a way to use {} in FOO to remove the tilde character as we usually do with bash substrings
e.g. :
$ echo ${string%substring}
I aim ...
6
votes
2
answers
2k
views
Using literal empty curly braces {} inside sed command from find -exec
I'm wondering if it is possible to include empty curly braces {} inside a sed replacement called from a find -exec.
An example:
find "$dir" -type f -name "*" -exec sed -i s/hello{}a/hello{}b/g '{}' +...
-1
votes
2
answers
778
views
bash: Deleting directories not containing given strings
How can I delete directories using the bash that do not contain directories named wav or mp3? I use macOS Sierra.
find . -type d \! -exec test -e '{}/wav' \; -print finds the directories not ...
2
votes
1
answer
3k
views
Find files by change date [duplicate]
I'd like to search recursively for files with a specific Change date. Note, this is not the Access or Modify date, but the Change date as outputted by stat.
#stat prototype.js
File: `prototype.js'
...
0
votes
1
answer
1k
views
Mute gzip errors/warnings when using zgrep
I frequently use the commands like
find . ! -iname "*test*" -o -iname "*foo*" | xargs zgrep -ie first.*last -e last.*first
I use zgrep because it can grep through .gz files, and if the files aren't ...
0
votes
2
answers
4k
views
Finding unmanaged files that are older than 6 months
I'm trying to find all files that haven't been touched for 6 months or longer. I ran this command but it just shows all files that were edited 6 months a go.
find . -type f -mtime 183 -exec ls -latr ...
1
vote
0
answers
279
views
Find directory containing file; execute command in that dir; then delete files
I want to find all directories (recursively) which contain .cue files, execute a command in that directory, then delete all files in that directory with the same prefix as the .cue file.
find . -name ...
2
votes
1
answer
1k
views
Combining basename {} and string's operations in bash
I want to write a script to find all the files ending with .cpp and rename them as .cc. The search will be done recursively from a start route R, which is the script's argument.
If no argument is ...
1
vote
2
answers
1k
views
Using mv to move to another directory and rename at once
I would like to know every file which is +100Mb and hasn't been accessed in the last month, and I have written succesfully:
find / -size +100M -atime +30
And now I want to move those files to a ...
3
votes
2
answers
2k
views
Remove 50GB oldest files in busybox when used capacity reaches 95%
Ok I have requested a code here but initial I didn't ask to make it busybox compatible. My bad. I'm new to Linux and coding.
The code needs to do the following:
Delete 50GB of oldest data (dir ...
6
votes
2
answers
7k
views
Find files by character encoding
I have a long-running python script that failed to utf-8 decode a file. The error message doesn't tell me what file it failed on, only that it couldn't decode byte 0x81 in position 194. I know which ...
3
votes
3
answers
12k
views
How can I pass strings with single quotes to grep?
My desired outcome is the following: to recursively search a directory looking for a given string in all found files. The following command is my usual port of call:
find ./ | xargs grep -ns 'foobar'
...
3
votes
2
answers
6k
views
Delete multiple patterns of files using one command (find)
I am trying to find and delete files in current directory and subdirectories (recursively) which match different patterns and print the matching files to stdout to know which ones are deleted.
For ...
2
votes
3
answers
6k
views
Find to list all files with date pattern to tar them together
I have a directory with many log files, with filenames like acct.20111001 up to acct.20111030. I want to zip them by running:
tar -cvjf acct.20111001.bz2 acct.20111001
How can I use find to ...
3
votes
1
answer
8k
views
find log files older than 30 days period
I'm working on a script to find files older than 30 days in a given folder. Based on requirement i have to delete only files with extension .log or .out and skip sub directories.
The below command ...
2
votes
4
answers
4k
views
Find size of directories recursively and get a total
I am using this command to find the folders I want and count up the size.
find . -type d -name 'tmp_c*' | xargs du -hcs {} \; +
My version of find does not support -exec. But, this works. However ...
2
votes
3
answers
2k
views
Batch rename folders with a single bash command
I have folders setup like this:
/path/to/directory/SLUG_1/SLUG_1 - SLUG_2 - SLUG_3
SLUG_2 is a year, and it may have a letter after the year, like "1994" or "2003a".
I would like to rename those ...
1
vote
0
answers
55
views
Find files inside sub-directories with different names (to remove them with mtime)
I have a set of client folders on a server. Each client has backups inside a "backup" directory like so;
~/backups/<CLIENT_NAME>/{daily, weekly, monthly}/{databases, directories, logs}
I'd ...
1
vote
1
answer
867
views
How would I find uneven file permissions within a directory structure?
How could I go about finding uneven file/directory permissions within a directory structure? I've made some attempts at using the find command similar to:
find /bin ! \( -perm 777 -o -perm 776 -o -...
1
vote
4
answers
251
views
How do I find a word in a file and insert text two lines below that?
I have a file that I want to find a key word in and enter text 2 lines down.
For example lets say the file i have contains the following words
the
cow
goes
moo
I want to be able to find the word "...
0
votes
1
answer
169
views
move file based on iso8601 date format that is 1 week old
I have a set of backups that run on a cron job every 4 hours (00:00, 04:00...etc). These files are named based on;
NOW=$(date +%F-%a-%H.%M)-filename which translates to 2016-10-25-Tue-04.00-filename.
...
-2
votes
2
answers
876
views
cut a text between / / symbols
i have some below strings
2-0 854 0/413/5963 W 65.19 0 0 0.0 4.76 115.86
::1 www.example.com GET /server-status HTTP/1.1
2-0 854 0/413/5963 W 65.19 0 0 0.0 4.76 115.86
...
12
votes
3
answers
11k
views
How to pass files found by find as arguments?
First to cut off trivial but inapplicable answers: I can use neither the find+xargs trick nor its variants (like find with -exec) because I need to use few such expressions per call. I will get back ...
1
vote
3
answers
3k
views
Command find and tar in script
I'm trying write a script which requires user interaction. Basically, the script has to ask user about 3 parameters: name of the file(s) which user wants to find, the number of days during which the ...
-1
votes
1
answer
102
views
finding specific directories
I need to find all directories that begin with dirD in the directory /dirA/dirB/dirC. Basically, dirD*. So far I tried:
find . -type d -name "dirD*"
and
ls -lrt | grep "^d" | grep "dirD*"
Now I'm ...
4
votes
2
answers
25k
views
find and regex - find files with names starting with given name
I need a regex expression to use along with find to find all the files which name start with a given string, for example proc.
I tried with find . -regex '^proc*' but it gives me no results.
2
votes
1
answer
12k
views
Find all tar.gz files and move them to a one level down directory
I have tar.gz files accessible from
./parent/subfolder/tar_file_folder/*tar.gz
And I want to find them and move them into the ./parent/ directory, therefore one level up. However, there are several ...
2
votes
2
answers
305
views
find command: using {} in a path
I accidentally copied the folders in my Music directory to the parent michal directory. I want to delete these directories in one fell swoop.
Is there any way to make the following work? Or is there ...
2
votes
1
answer
536
views
Grep output between XML nodes "Pattern within the main node set"
I am wondering if there is any way to accomplish this.
XML Format:
<Main>
<Master>
<Node1>Content</Node1>
<Node2>qwerty</Node2>
<Node3>854789</Node3>
...