How to rename files in different directories with the same name using find - linux

I have files named test.txt in different directories like this
./222/test.txt
./111/test.txt
I want to rename all test.txt to info.txt
I've tried using this
find . -type f -iname 'test.txt' -exec mv {} {}info \;
I get test.txtinfo

Your idea is right, but you need to use -execdir instead of just -exec to simplify this.
find . -type f -iname 'test.txt' -execdir mv {} info.txt ';'
This works like -exec with the difference that the given shell command is executed with the directory of the found pathname as its current working directory and that {} will contain the basename of the found pathname without its path. Also note that the option is a non-standard one (non POSIX compliant).

Related

Linux move files from dir to dir by name mask

I am trying to move all files with names starts with SML from directory to another.
Tried with
find /var/.../Images/ -name SML\* mv /var/.../Images/Small but doesnt work
try find /var/.../Images/ -name SML\* -exec mv {} /var/.../Images/Small/ \;
I guess you want something like this:
dir=/path/to/your/Images
mkdir -p "$dir/Small"
find "$dir" -name "SML*" -not -wholename "$dir/Small/*" -exec mv {} "$dir/Small/" \;
Since the directory you move the files to is a subdirectory of the one you seach in, you need to exclude the files already moved there. So I added -not -wholename "$dir/Small/*"
To execute a command for each found file, you need -exec .... The alternative would be to pipe your find results to a while read loop.
When using -exec, the found name can be referenced by {}.
See man find for a lot more information.

Copy recursive files of all the subdirectories

I want to copy all the log files from a directory which does not contain log files, but it contains other subdirectories with log files. These subdirectories also contain other subdirectories, so I need something recursive.
I tried
cp -R *.log /destination
But it doesn't work because the first directory does not contains log files. The response can be also a loop in bash.
find /path/to/logdir -type f -name "*.log" |xargs -I {} cp {} /path/to/destinationdir
Explanation:
find searches recursively
-type f tells you to search for files
-name specifies the name pattern
xargs executes commands
-I {} indicates an argument substitution symbol
Another version without xargs:
find /path/to/logdir -type f -name '* .log' -exec cp '{}' /path/to/destinationdir \;

Bash: Find files containing a certain string and copy them into a folder

What I want:
In a bash script: Find all files in current directory that contain a certain string "teststring" and cop them into a subfolder "./testfolder"
Found this to find the filenames which im looking for
find . -type f -print0 | xargs -0 grep -l "teststring"
..and this to copy found files to another folder (here selecting by strings in filename):
find . -type f -iname "stringinfilename" -exec cp {} ./testfolder/ \;
Whats the best way to combine both commands to achieve what I described at the top?
Just let find do both:
find . -name subdir -prune -o -type f -exec \
grep -q teststring "{}" \; -exec cp "{}" subdir \;
Note that things like this are much easier if you don't try to add to the directory you're working in. In other words, write to a sibling dir instead of writing to a subdirectory. If you want to wind up with the data in a subdir, mv it when you're done. That way, you don't have to worry about the prune (ie, you don't have to worry about find descending into the subdir and attempting to duplicate the work).

How to find and rename all files using -exec

I want to rename all files which end on ".mp4" in such way that instead of white spaces to contain underscores.
Example:
Original file -> test 1.mp4
Renamed file -> test_1.mp4
I was trying with:
find . -iname "*.mp4" -exec mv {} $(echo '{}' | tr " " "_") \;
But I got only:
mv: ‘./test 1.mp4’ and ‘./test 1.mp4’ are the same file
It seems that my pipe is not working.I would appreciate all ideas.
It seems that the only way to do it so far is via "rename" command.
Here is the command that worked:
find /tmp/test/ -iname "*.mp4" -exec rename " " "_" "{}" \;
I'd look into using the rename command, assuming your distribution provides it. In Debian: https://packages.debian.org/rename
If you don't need to search in subdirectories:
rename 's/\ /_/' *.mp4
If you do:
find . -iname "*.mp4" -execdir rename 's/\ /_/' {} \;
If multiple spaces must be replaced, the search/replace expression becomes 's/\ /_/g'
If you really want to keep your syntax/approach, see this very similar question: find -exec with multiple commands

In Unix,cmd to search a file recursively and retrieve the file instead of just the path of the file

In Unix, what is the single cmd that lets me search and locate a file recursively and then retrieve the file instead of just the path of the file?
What do you mean by retrieve?
You can simply use -exec argument to find.
$ find /path/to/search -type f -name '*.txt' -exec cat {} \;
$ find /path/to/search -type f -name 'pattern' -exec cp {} /path/to/new \;
The second one should work.
cat `find /wherever/you/want/to/start/from -name name_of_file`
Note those quotes are backquotes (`).

Resources