linux command line subfolder files batch rename that actually works - linux

In a folder with many subfolders, each containing mp4 files with names ending in foobar.mp4
How can I get rid of the suffix? I have tried:
find ./*/*.mp4 -type f -exec rename 's/foobar//' '{}' \;

On surface, there is no need to use 'find', as the rename command has the ability to process list of file. In particular, possible to write
rename 's/foobar//' */*foobar.mp4
It's not clear from the OP what are the actual file names. The above command will rename 'foobar.mp4' to hidden file '.mp4', and the file 'x.foobar.mp4' to 'x..mp4'.

Related

Copy or move all files in a directory regardles of folder depth or number

Lets say i have a folder named Pictures and I want to move or copy all files out of this folder.
However I also want to move and harvest all of the files who are in sub folders so:
Pictures/1.png
Pictures/yolo/2.png
Pictures/yolo/swag/sand/3.png
Pictures/extra/fire/4.png
I want to move or copy all these files to another folder like results so I get:
results/1.png
results/2.png
results/3.png
results/4.png
Only I have no idea in advance what sub folders will be in the Pictures folder.
How can I accomplish this in bash/shell scripts ?
I also appreciate making it file type neutral so any files are harvested from their directories (not only .png like in my example) and I have no idea what the file name will be (I only used 1...4 because i did not have any idea how to name them).
You can do it like this:
find /absolute/path/to/Pictures -type f -name '*.png' -exec mv -i {} /absolute/path/to/results \;
Another option is to use xargs
find /absolute/path/to/Pictures -name '*.png' | xargs -I files mv files /absolute/path/to/results
You can simply copy all files and subdirectories along with their contents using cp's recursive option:
cp -pr <source_path>/* <destination_path>/
But, moving them recursively is a bit tricky, you will need to create tar files of the subdirectories and move them and then untar the tar files in destination path. As this is a complex process, as a workaround, you can copy the files/directories recursively and then delete the files from original path.
cp -pr <source_path>/* <destination_path>/ && rm -rf <source_path>/*

Recursive find that will append directory name to any file

Any help would be greatly appreciated!
I need a way to append the parent directory name to any file in any path.
An example current directory tree
/Hawaii/Surfing/800x600/picture1.jpg
/Hawaii/Surfing/800x600/picture2.jpg
/Hawaii/Surfing/800x600/picture3.jpg
/RockClimbing/SouthAfrica/TableMountain/4096x2160/Picture1.jpg
/RockClimbing/SouthAfrica/TableMountain/4096x2160/Picture2.jpg
/RockClimbing/SouthAfrica/TableMountain/4096x2160/Picture3.jpg
The goal
/Hawaii/Surfing/800x600/picture1.800x600.jpg
/Hawaii/Surfing/800x600/picture2.800x600.jpg
/Hawaii/Surfing/800x600/picture3.800x600.jpg
/RockClimbing/SouthAfrica/TableMountain/4096x2160/Picture1.4096x2160.jpg
/RockClimbing/SouthAfrica/TableMountain/4096x2160/Picture2.4096x2160.jpg
/RockClimbing/SouthAfrica/TableMountain/4096x2160/Picture3.4096x2160.jpg
I have found some examples of this but the users all have set directory depths unfortunately I have files at many different levels.
find dir -name *.jpg -exec rename -nv -- 's|/(.*)/(.*)$|/$1/$1.jpg|' {}
Your first capture group is matching everything before the last /, not just the last directory name. Use /([^/]*)/ instead of /(.*)/ so it won't match across / delimiters. You're also not splitting up the filename into the name and extension, so you're not inserting the directory name between them.
find dir -name *.jpg -exec rename -nv -- 's|([^/]*)/([^/]*)\.jpg$|$1/$2.$1.jpg|' {} +

Using rename to rename files and directories

I'm using the Linux rename command line tool to search recursively through a directory to rename any directories as well as files it finds. The issue I'm running into is the rename command will rename a sub-directory of a file then attempt to rename the parent directory of the same file. This will fail because the sub-directory has been renamed resulting in a "No such file or directory"
Command:
rename -f 's/foo/bar/' **
rename -f 's/Foo/Bar/' **
For example, here is an original file that I would like to replace 'foo' with 'bar'
File:
/test/foo/com/test/foo/FooMain.java
Failure:
Can't rename /test/foo/com/test/foo/FooMain.java /test/bar/com/test/foo/FooMain.java: No such file or directory
Preferred File:
/test/bar/com/test/bar/BarMain.java
You can see from the error message that it's attempting to rename the parent directory but at that point the subdirectory has already been changed resulting in the file not found error. Is there parameters for the rename command that will fix this or do I need to go about this in a different way?
Use find + execdir option:
$ find . -execdir rename "s/foo/bar/" {} \;
$ find . -execdir rename "s/Foo/Bar/" {} \;

Moving files without overwrite

I'm using the following command to move all files in subfolders to a destination folder, without overwriting files with same name:
find folder-target -type f -exec cp --backup=numbered \{\} folder-final \;
And this is causing the files append ~1~ if the file already exists. The problem is: this is causing the file usuless. I need catch all my pdfs, and i can't open this pdfs if they have this numbers.
Is this fixable? Can't i use a pre-fix?
Thanks.
try cp -n
see man pages here : http://man7.org/linux/man-pages/man1/cp.1.html

Extract all the files with name containing a keyword

I have thousands of html file in a directory. I wanna extract files that contain Chennai in the file name and put it into another folder. I am sure it is possible. I am not close enough to copy the files to another folder.
Use globbing:
mv *Chennai* target/
If the file names might start with a dot, use
mv .*Chennai* *Chennai* target/
Try:
find directory_with_htmls -type f -name "*Chennai*.html" -exec cp {} some_other_folder \;
This would copy html files in the directory_with_htmls directory containing Chennai in the name to the directory some_other_folder.

Resources