rename multiple files at once and keep their backup - rename

I need to rename many files at once and at the same time want to keep original files as it is.
For renaming I can use
rename "s/Nov2012/Oct2014/g" *.conf
But I am losing the files Nov2012. How can I keep them also?

Copy them first (to a temporal directory, for example), and then rename:
> mkdir tmp
> cp "*Nov2012*.conf" tmp
> cd tmp
> rename "s/Nov2012/Oct2014/g" *.conf
> mv * ..
> cd ..
> rmdir tmp

Related

Zipping contents of folder includes parent directories

zip -r $packageName.zip /home/ubuntu/backend/upgrade/temp
I expect $packageName.zip to have all the contents in temp folder. However, I'm getting /home/ubuntu/backend/upgrade/contents in my zip folder.
I've tried
pushd /home/ubuntu/backend/upgrade/temp
zip -j /home/ubuntu/backend/test.zip ./*
popd
and
pushd /home/ubuntu/backend/upgrade/temp
zip -r /home/ubuntu/backend/test.zip ./*
popd
This should work:
zip -rj $packageName.zip /home/ubuntu/backend/upgrade/temp
You need the recursive portion of the command combined with the relative paths.

Remove directory structure, but keep directory contents

I meant to run this code inside a specific directory within my home directory, but accidentally ran it in my home directory itself:
i=0; for f in *; do d=dir_$(printf %03d $((i/8+1))); mkdir -p $d; mv "$f" $d; let i++; done
Now all my files and directories have been grouped into separate directories. I need to remove this action, and restore my original organization. Is this possible?
Using shell expansion:
mv dir_*/* .
should move the content of all dir_ subdirectories back into the current one. For "hidden" files, if necessary, move dir_*/.*.

What is the linux command to move the files of subdirecties into one level up respectively

The path structure of the files on my server is similar to shown below,
/home/sun/sdir1/mp4/file.mp4
/home/sun/collection/sdir2/mp4/file.mp4
I would like to move the files of "mp4" into one level up(into sdir1 and sdir2 respectively)
So the output should be,
/home/sun/sdir1/file.mp4
/home/sun/collection/sdir2/file.mp4
I have no idea to do this, so not tried yet anything...
There are different ways to solve your problem
If you just want to move those specific files, run these commands:
cd /home/sun/
mv sdir1/mp4/file.mp4 sdir1/
mv sdir2/mp4/file.mp4 sdir2/
If you want to move all mp4 files on those directories (sdir1 and sdir2), run these commands:
cd /home/sun/
mv sdir1/mp4/*.mp4 sdir1/
mv sdir2/mp4/*.mp4 sdir2/
Edit:
Make a script that iterates all the directories:
Create a script and name it and edit it with your favorite editor (nano, vim, gedit, ...):
gedit folderIterator.sh
The script file content is:
#/bin/bash
# Go to the desired directory
cd /home/sun/
# Do an action over all the subdirectories in the folder
for dir in /home/sun/*/
do
dir=${dir%*/}
mv "$dir"/mp4/*.mp4 "$dir"/
# If you want to remove the subdirectory after moving the files, uncomment the following line
# rm -rf "$dir"
done
Save the file and give it execute permissions:
chmod +x folderIterator.sh
And execute it:
./folderIterator.sh
You can do this:
# move all .mp4 files from sdir1/mp4 to sdir1 directory
user#host:~/home/sun$ mv sdir1/mp4/*.mp4 sdir/
# move all .mp4 files from collection/sdir2/mp4 to collection/sdir2 directory
user#host:~/home/sun$ mv collection/sdir2/mp4/*.mp4 collection/sdir2/
# move only 1 file
user#host:~/home/sun$ mv sdir1/mp4/file.mp4 sdir/
user#host:~/home/sun$ mv collection/sdir2/mp4/file.mp4 collection/sdir2/
I suggest you use find and something like
cd /home/sun/sdir1/mp4/
find . -name "*" -exec mv {} /home/sun/sdir1/ \;
cd /home/sun/collection/sdir2/mp4/
find . -name "*" -exec mv {} /home/sun/collection/sdir2/ \;
Alternatively, you could use tar and something like
cd /home/sun/sdir1/mp4/
tar cfp - * | (cd ../ ; tar xvvf -)
# Make sure everything looks good
rm -rf mp4
cd /home/sun/collection/sdir2/mp4/
tar cfp - * | (cd ../ ; tar xvvf -)
# Make sure everything looks good
rm -rf mp4
The command to move a file (or directory) up one level is:
mv /home/sun/sdir1/mp4/file.mp4 ..
Wildcards can be used to select more files & directories, you can also provide more than one directory at a time.
mv /home/sun/sdir1/mp4/*.mp4 /home/sun/collection/sdir2/mp4/*.mp4 ..

mv: cannot overwrite directory with non-directory

Is it possible to get around this problem?
I have a situation where I need to move some files to 1 directory below.
/a/b/c/d/e/f/g
problem is that the filename inside g/ directory is the same as the directory name
and I receive the following error:
mv: cannot overwrite directory `../297534' with non-directory
Example:
/home/user/data/doc/version/3766/297534 is a directory, inside there is a also a file named 297534
so I need to move this file to be inside /home/user/data/doc/version/3766
Command
This is what I am running: (in a for loop)
cd /home/user/data/doc/version/3766/297534
mv * ../
You can't force mv to overwrite a directory with a file with the same name. You'll need to remove that file before you use your mv command.
Add one more layer in your loop.
Replace mv * ../ with
for f in `ls`; do rm -rf ../$f; mv $f ..; done
This will ensure that any conflict will be deleted first, assuming that you don't care about the directory you're overwriting.
Note that this will blow up if you happen to have a file inside the current directory which matches the current directory's name. For example, if you're in /home/user/data/doc/version/3766/297534 and you're trying to move a directory called 297534 up. One workaround to this is to add a long suffix to every file, so there's little chance of a match
for f in `ls`; do mv $f ../${f}_abcdefg; done

Shell script - move files into folder

Suppose a particular command generates few files (I dont know the name of these files). I want to move those files into a new folder. How to do it in shell script?
i can't use :
#!/bin/bash
mkdir newfolder
command
mv * newfolder
as the cwd contains lot of other files as well.
The first question is can you just run command with newfolder as the current directory to generate the files in the right place it begin with:
mkdir newfolder
cd newfolder
command
Or if command is not in the path:
mkdir newfolder
cd newfolder
../command
If you can't do this then you'll need to capture lists of before and after files and compare. An inelegant way of doing this would be as follows:
# Make sure before.txt is in the before list so it isn't in the list of new files
touch before.txt
# Capture the files before the command
ls -1 > before.txt
# Run the command
command
# Capture the list of files after
ls -1 > after.txt
# Use diff to compare the lists, only printing new entries
NEWFILES=`diff --old-line-format="" --unchanged-line-format="" --new-line-format="%l " before.txt after.txt`
# Remove our temporary files
rm before.txt after.txt
# Move the files to the new folder
mkdir newfolder
mv $NEWFILES newfolder
use pattern matching:
$ ls *.jpg # List all JPEG files
$ ls ?.jpg # List JPEG files with 1 char names (eg a.jpg, 1.jpg)
$ rm [A-Z]*.jpg # Remove JPEG files that start with a capital letter
Example shamelessly taken from here where you can find some more useful information about it.
If you'd like to move them into a sub-folder:
mv `find . -type f -maxdepth 1` newfolder
Setting a -maxdepth 1 will only find the files in the current directory and will not recurse. Passing in -type f means "find all files" ("d" would, respectively, mean "find all directories").
Assuming that your command prints out names with one per line, this script will work.
my_command | xargs -I {} mv -t "$dest_dir" {}

Resources