Linux Bash: Move Multiple Different Files into Same Directory

Linux Bash: Move multiple different files into same directory

You can do

mv car.txt bicycle.txt vehicle/

(Note that the / above is unnecessary, I include it merely to ensure that vehicle is a directory.)

You can test this as follows:

cd               #Move to home directory
mkdir temp #Make a temporary directory
touch a b c d #Make test (empty) files ('touch' also updates the modification date of an existing file to the current time)
ls #Verify everything is there
mv a b c d temp/ #Move files into temp
ls #See? They are gone.
ls temp/ #Oh, there they are!
rm -rf temp/ #DESTROY (Be very, very careful with this command)

How can I move multiple files to a directory while changing their names and extensions using bash?

No need for the loop, you can do this with just rename and mv:

rename -v 's/$/.pgp/' /opt/dir/ABC/allfile_123*
rename -v s/allfile_123/new_name/ /opt/dir/ABC/allfile_123*
mv /opt/dir/ABC/new_name* /usr/tst/output/

But I'm not sure the rename you are using is the same as mine.
However,
since the replacement you want to perform is fairly simple,
it's easy to do in pure Bash:

for file in /opt/dir/ABC/allfile_123*; do
newname=new_name${file##*allfile_123}.gpg
mv "$file" /usr/tst/output/"$newname"
done

If you want to write it on a single line:

for file in /opt/dir/ABC/allfile_123*; do newname=new_name${file##*allfile_123}.gpg; mv "$file" /usr/tst/output/"$newname"; done

Bash: Move multiple files with same name into same directory by renaming

You could do something like this (either in a script or right on the command line):

for i in A B C D E  
do
mv Folder$i/file.txt NewFolder/file_$i.txt
done

It won't convert letters to numbers, but it does the basics of what you're looking for in a fairly simple fashion.

Hope this helps.

How do you move files of specific file extension in multiple directories, one directory deeper within their respective directories?

You can do what you need with find that searchers for all *.jpg files and a simple helper script called by the -exec option to find to create the jpg directory and move all .jpg files into the new directory.

The helper script will simply get the absolute filename utilitzing readlink -f and then using quick parameter expansion to trim the last /... component from the absolute filename to obtain the full path. Then it is simply a matter of creating the jpg directory at the end of the path and moving the file to the new directory.

Your helper script (I called it helper.sh) could be:

#!/bin/sh

test -z "$1" && exit ## validate 1 argument given or exit

full=$(readlink -f "$1") ## get full filename
dir="${full%/*}" ## get full path

test "${full##*.}" = 'jpg' || exit ## test extension is jpg or exit

test -z "$dir" && dir="/" ## check if file was in / (root)

test -d "$dir/jpg" || mkdir -p "$dir/jpg" ## check/create jpg dir at end of path

mv "$full" "$dir/jpg" ## move file into new jpg dir

(note: after creating the helper script, make sure you make it executable with chmod +x helper.sh)

Original Projects Directory Tree

$ tree Projects/
Projects/
├── Project_001
│   └── Image_001.jpg
├── Project_002
│   ├── Image_002.jpg
│   └── Image_003.jpg
└── Project_003

Your find command operating on the Projects directory calling the helper script for each file would be:

$ find Projects/ -type f -name "*jpg" -exec ./helper.sh '{}' \;

Resulting Projects Directory Tree

$ tree Projects/
Projects/
├── Project_001
│   └── jpg
│   └── Image_001.jpg
├── Project_002
│   └── jpg
│   ├── Image_002.jpg
│   └── Image_003.jpg
└── Project_003

Let me know if you have further questions.

Preserving Files Already In jpg Directory

Per-your additional comment, in order to preserve .jpg files already within a jpg directory under your Projects, all you need to do is add one additional check. If the last component of the path is already jpg, just exit the helper, e.g.

test "${dir##*/}" = 'jpg' && exit           ## if already in jpg dir, exit

Shown in context in helper.sh:

test -z "$1" && exit                        ## validate 1 argument given or exit

full=$(readlink -f "$1") ## get full filename
dir="${full%/*}" ## get full path (trim last /*)

test "${dir##*/}" = 'jpg' && exit ## if already in jpg dir, exit
test "${full##*.}" = 'jpg' || exit ## test extension is jpg or exit
...

Original Projects Directory Tree (w/existing jpg)

$ tree Projects/
Projects/
├── Project_001
│   ├── Image_001.jpg
│   └── jpg
│   └── Image_000.jpg
├── Project_002
│   ├── Image_002.jpg
│   ├── Image_003.jpg
│   └── jpg
│   ├── Image_000.jpg
│   └── Image_001.jpg
└── Project_003

Resulting Projects Directory Tree

$ tree Projects/
Projects/
├── Project_001
│   └── jpg
│   ├── Image_000.jpg
│   └── Image_001.jpg
├── Project_002
│   └── jpg
│   ├── Image_000.jpg
│   ├── Image_001.jpg
│   ├── Image_002.jpg
│   └── Image_003.jpg
└── Project_003

Shell: How to move multiple files to a directory and zip that directory with different name?

Here is my current solution, it's a small script and I'm not too satisfied with it, but it works. I will accept if someone answers with more elegant solution. So, here it is:

#clear the contents of the previous build first
rm -r build/*

#copy required files to a temporary folder
rsync -av --exclude=build ./ build/project

#change current directory to build
cd build/

#zip files from build/project folder
zip -r "project-04.05.2016" project

#remove temporary folder
rm -r project/

#final zip file is at location:
#build/project-04.05.2016.zip

Linux moving or copying multiple files with a shell

What about cp *.txt /dest/dir/?

And for adding .backup you could also do a loop that could look like this:

for i in *.txt
do
cp "$i" "/dest/dir/$i.backup"
done

Moving multiple files to multiple directories based on filename pattern, while excluding some

I'm assuming that there are more that two 7-letter sequences at the beginning of your filenames (because otherwise you could just mv commands with bash extension). On bash, fist move the 'special' files with the letter sequence in the middle to the new directory:

cd  /data/myowndir/
for filename in *sT13*.*; do
if [ "${filename: -4}" != ".nii" ]; then
dir_name=${filename:0:5};
mkdir -p ${dir_name}struc
mv -i $filename ${dir_name}struc/${filename}
fi
done

then move all the other files to their respective new folders:

for filename in *.*; do
if [ "${filename: -4}" != ".nii" ]; then
dir_name=${filename:0:7};
mkdir -p $dir_name
mv -i $filename $dir_name/${filename}
fi
done

the -p option for the mkdir command assures that you don't get errors if the directory already exists. ${string:0:7} selects the first 7 letters of a string



Related Topics



Leave a reply



Submit