Extract all the files with name containing a keyword - linux

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.

Related

How to rename multiple files, contained in multiple sub-directories

I have a parent directory that contains multiple subdirectories, and each subdirectory has multiple files. For example:
Sub-directory_1
xyz1.fq.gz
xyz2.fq.gz
Sub-directory_2
abc1.fq.gz
abc2.fq.gz
etc.
I'd like to add the subdirectory name, as a prefix, to each file in each subdirectory.
I've been trying to do this with a loop that uses subdir and FILENAME, but can't get it right. Any help is appreciated.
You can use find command:
$ find . -type f -exec bash -c 'mv {} "$(dirname {})/$(dirname {})_$(basename {})"' \;

Shell - create zip files in directories without including subdirectories

I have a structure like this:
file1.jpg
file2.png
subfolder1
subfolder2
file3.jpg
...
now I would like to create a zip file via shell only of the files present in the root of the directory without taking into consideration the subdirectories and the files contained within them.
How can I do? I hope I have been clear enough
You can try something like this by using a find.
It will only find files that are in the current folder and zip it to a file called filename.zip
find . -maxdepth 1 -type f -exec zip filename.zip '{}' \;

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>/*

Copy or move from nth level subfolder to another directory or folder

We have a software that adds a wav file and put it on a folder by its date which is buried under several subfolders.
For example:
home/user/music/group1/person1/todays date/wav file
home/user/music/group1/person1/yesterdays date/wav file
home/user/music/group1/person2/todays date/wav file
home/user/music/group1/person2/yesterdays date/wav file
Also, the person(n) folder is dynamic which means its created automatically if the software founds someone using that device and creates that folder. So for example, if a new user is using the software it will create home/user/music/group1/person3/.
How do I move or copy starting from the person(n) folder and move them to a new folder like home/user/new/person1.. home/user/new/person2..
Since the person(n) folder is dynamic I could not just do command like cp person1 newdirectory
What i did is find all wav files under group1 folder and cp to new folder but it copies the full path.
find /home/user/music/group1 -name "*.wav" -type f -exec cp --parents \{\} /home/user/new \;
If i remove --parents it will only copy the files to new folder. how do I copy starting from the person(n) folder to new folder?
thanks #RamanSailopal, i got it to work with find /home/user/music/group1/ -name "person*" -type d -exec cp -pR {}/ /home/user/new \;

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|' {} +

Resources