I have a folder with a number of subfolders. Each of the subfolders consists of several files without extension.
I need to add extension .cel to each file in subfolders.
How can I do it using bash?
find to the rescue:
find /your/folder -type f -exec mv {} {}.cel \;
Explanation: find obtains all files inside the /your/folder structure. From all the results obtained, it performs the mv command. It makes the file XXX to be moved to XXX.cel, which is another way of renaming it.
If you have rename then using that with find should do the trick:
find . -type f -exec rename -v 's/$/\.cel/' {} \;
Related
I am trying to copy all empty files from the home directory into a folder that is on the desktop, using this:
find ~ -empty -exec cp {} /desktop/emptyfolder \;
However, I can't make it work.
Are there any other possible solutions to achieve this? Or maybe to write a bash script that could do this?
Add -type f to the find command to force it to search for files and not directories and so:
find ~ -empty -type f -exec cp {} /desktop/emptyfolder \;
I have a complicated scenario. In my current working directory, I have several subdirectories. Each subdirectory has a number of files, but I'm only interested in one: RAxML_bestTree.best. The file name is the same for each corresponding file in every subdirectory, i.e., they are not unique. Thus, a copy command to a new subdirectory will not work since one RAxML_bestTree.best will be shown and overwritten 514 times.
I need to take the content of each subdirectory's RAxML_bestTree.best and have it placed into a file all_RAxML_bestTrees.txt either in the current working directory or a new subdirectory. I have tried the following, which appears to print the contents to screen but not to file:
find . -type f -name \RAxML_bestTree.best -exec cat {} all_RAxML_bestTrees.txt \;
Nevermind, found my issue:
find . -type f -name \RAxML_bestTree.best -exec cat > all_RAxML_bestTrees.txt \;
I have a large series of directories and subdirectories with many jpgs in them. I need to do two things:
Create a copy of each jpg found within it's own subdirectory
Rename this copied jpg such that apple.jpg becomes apple_m.jpg
I have tried to run the following commands in order:
//this creates a copy of every jpg found and appends '_m' to the end of it
find . -name '*.jpg' -execdir cp {} {}_m \;
//now again find everything with 'jpg_m' at the end and rename it as filename_m.jpg
find . -name '*.jpg_m' -execdir rename -v 's/\.jpg_m/_m\.jpg/' {} \;
However the 2nd command does'nt seem to work, can someone please explain what I'm doing wrong?
Thanks,
Adi
To rename files, you can simply use it this way
rename ".jpg_m" "_m.jpg" files_to_rename
so, if you want to copy and rename, you can rename it this way:
find . -name "*.jpg_m" -execdir rename ".jpg_m" "_m.jpg" {} \;
Why don't you do it in one pass?
find . -name '*.jpg' -exec cp {} m_{} \; -exec rename "m_" "" {} \;
I want to copy just the files i've created/edited today into a separate directory "changeset" whilst maintaining their directory structure
I came up with the following script
cd ./myproject/
find ./* -mtime -1 -daystart -exec cp {} ../changeset/{} \;
The drawbacks of the above is that directories aren't created and the copy throws an error.
I've manually gone into ../changeset/ and create the folder structure until the command runs without errors.. but thats a little tedious.
Is there a simple solution to this?
find * -mtime -1 -daystart -print0 | cpio -pd0 ../changeset
cpio is an old, oddball archival program that is occasionally the best tool for the job. With -p it copies files named on stdin to another directory. With -d it creates directories as needed.
I've found another solution which isn't as elegant as John's but which isn't reliant on cpio, which i dont have.
cd ./myproject/
# Create all directories
find ./* -type d -exec mkdir ../changeset/{} \;
# Copy files
find ./* -mtime -1 -daystart -exec cp {} ../changeset/{} \;
# Delete empty directories, run this several times because after moving a child the parent directory needs to be removed
find ../changeset/ -type d -empty -exec rmdir {} \;
I have some folder hierarchy, in some of the folders there are images, I need a shell script which can list all images and copy them into one specified folder, where listing them is not important, I just want to copy all images into a folder?
I know I can
ls -R *.png
but how do I copy them all to one folder?
Thanks!
Update: As glenn jackman has pointed out, this would be slightly more efficient to use over the answer I provided:
file . -type f -name \*.png | xargs cp -t destination
For the explanation, see glenn's comments that follow this answer.
One way is to use find:
find . -type f -name "*.png" -exec cp {} ~/path/to/your/destination/folder \;
Explanation:
find is used to find files / directories
. start finding from the current working directory (alternatively, you can specify a path)
-type f: only consider files (as opposed to directories)
-name "*.png": only consider those with png extension
-exec: for each such result found, do something (see below)
cp {} ~/path/to/your/destination/folder \;: this is the do something part: copy each such result found (substituted into the {}) to the destination specified.
To copy multiple file patterns in single go we can use -regex instead -name
find . -type f -regex '.*\(jpg\|jpeg\|png\|gif\|mp4\|avi\|svg\|mp3\|vob\)' -exec cp {} /path/to/your/destination/folder \;