Move Multiple sequence file using bash - linux

I want to move sequence 20 file in different folder
so i use below code to move file but not work...
echo "Moving {$(( $j*20 -19))..$(( $j*20 ))}.png ------- > Folder $i"
mv {$(( $j*20 -19))..$(( $j*20 ))}.png $i;
So i get output in terminal
Moving {1..20}.png ------- > Folder 1
mv: cannot stat ‘{1..20}.png’: No such file or directory
But there is already 1.png to 20.png image file + Folder...
So how to move sequence file like
{1..20}.png -> Folder 1
{21..40}.png -> Folder 2
Thank you!!!

I don't think that it is possible to combine brace expansion with arithmetic expressions as you are doing. Specifically, ranges like {a..b} must contain literal values, not variables.
I would suggest that instead, you used a for loop:
for ((n=j*20-19;n<=j*20;++n)); do mv "$n.png" "$i"; done
The disadvantage of the above approach is that mv is called many times, rather than once. As suggested in the comments (thanks chepner), you could use an array to reduce the number of calls:
files=()
for ((n=j*20-19;n<=j*20;++n)); do files+=( "$n.png" ); done
mv "${files[#]}" "$i"
"${files[#]}" is the full contents of the array, so all of the files are moved in one call to mv.

You have to evaluate the resulting string again with eval
For example
eval "mv {$(( $j*20 -19))..$(( $j*20 ))}.png folder$j"

Related

Batch copy and rename multiple files in the same directory

I have 20 files like:
01a_AAA_qwe.sh
01b_AAA_asd.sh
01c_AAA_zxc.sh
01d_AAA_rty.sh
...
Files have a similar format in their names. They begin with 01, and they have 01*AAA*.sh format.
I wish to copy and rename files in the same directory, changing the number 01 to 02, 03, 04, and 05:
02a_AAA_qwe.sh
02b_AAA_asd.sh
02c_AAA_zxc.sh
02d_AAA_rty.sh
...
03a_AAA_qwe.sh
03b_AAA_asd.sh
03c_AAA_zxc.sh
03d_AAA_rty.sh
...
04a_AAA_qwe.sh
04b_AAA_asd.sh
04c_AAA_zxc.sh
04d_AAA_rty.sh
...
05a_AAA_qwe.sh
05b_AAA_asd.sh
05c_AAA_zxc.sh
05d_AAA_rty.sh
...
I wish to copy 20 of 01*.sh files to 02*.sh, 03*.sh, and 04*.sh. This will make the total number of files to 100 in the folder.
I'm really not sure how can I achieve this. I was trying to use for loop in the bash script. But not even sure what should I need to select as a for loop index.
for i in {1..4}; do
cp 0${i}*.sh 0${i+1}*.sh
done
does not work.
There are going to be a lot of ways to slice-n-dice this one ...
One idea using a for loop, printf + brace expansion, and xargs:
for f in 01*.sh
do
printf "%s\n" {02..05} | xargs -r -I PFX cp ${f} PFX${f:2}
done
The same thing but saving the printf in a variable up front:
printf -v prefixes "%s\n" {02..05}
for f in 01*.sh
do
<<< "${prefixes}" xargs -r -I PFX cp ${f} PFX${f:2}
done
Another idea using a pair of for loops:
for f in 01*.sh
do
for i in {02..05}
do
cp "${f}" "${i}${f:2}"
done
done
Starting with:
$ ls -1 0*.sh
01a_AAA_qwe.sh
01b_AAA_asd.sh
01c_AAA_zxc.sh
01d_AAA_rty.sh
All of the proposed code snippets leave us with:
$ ls -1 0*.sh
01a_AAA_qwe.sh
01b_AAA_asd.sh
01c_AAA_zxc.sh
01d_AAA_rty.sh
02a_AAA_qwe.sh
02b_AAA_asd.sh
02c_AAA_zxc.sh
02d_AAA_rty.sh
03a_AAA_qwe.sh
03b_AAA_asd.sh
03c_AAA_zxc.sh
03d_AAA_rty.sh
04a_AAA_qwe.sh
04b_AAA_asd.sh
04c_AAA_zxc.sh
04d_AAA_rty.sh
05a_AAA_qwe.sh
05b_AAA_asd.sh
05c_AAA_zxc.sh
05d_AAA_rty.sh
NOTE: blank lines added for readability
You can't do multiple copies in a single cp command, except when copying a bunch of files to a single target directory. cp will not do the name mapping automatically. Wildcards are expanded by the shell, they're not seen by the commands themselves, so it's not possible for them to do pattern matching like this.
To add 1 to a variable, use $((i+1)).
You can use the shell substring expansion operator to get the part of the filename after the first two characters.
for i in {1..4}; do
for file in 0${i}*.sh; do
fileend=${file:2}
cp "$file" "0$((i+1))$fileend"
done
done

Linux bash How to use a result of a wildcard as a file name in a copy command

I'm writing a Linux script to copy files from a folder structure in to one folder. I want to use a varying folder name as the prefix of the file name.
My current script looks like this. But, I can't seem to find a way to use the folder name from the wildcard as the file name;
for f in /usr/share/storage/*/log/myfile.log*; do cp "$f" /myhome/docs/log/myfile.log; done
My existing folder structure/files as follows and I want the files copied as;
>/usr/share/storage/100/log/myfile.log --> /myhome/docs/log/100.log
>/usr/share/storage/100/log/myfile.log.1 --> /myhome/docs/log/100.log.1
>/usr/share/storage/102/log/myfile.log --> /myhome/docs/log/102.log
>/usr/share/storage/103/log/myfile.log --> /myhome/docs/log/103.log
>/usr/share/storage/103/log/myfile.log.1 --> /myhome/docs/log/103.log.1
>/usr/share/storage/103/log/myfile.log.2 --> /myhome/docs/log/103.log.2
You could use a regular expression match to extract the desired component, but it is probably easier to simply change to /usr/share/storage so that the desired component is always the first one on the path.
Once you do that, it's a simple matter of using various parameter expansion operators to extract the parts of paths and file names that you want to use.
cd /usr/share/storage
for f in */log/myfile.log*; do
pfx=${f%%/*} # 100, 102, etc
dest=$(basename "$f")
dest=$pfx.${dest#*.}
cp -- "$f" /myhome/docs/log/"$pfx.${dest#*.}"
done
One option is to wrap the for loop in another loop:
for d in /usr/share/storage/*; do
dir="$(basename "$d")"
for f in "$d"/log/myfile.log*; do
file="$(basename "$f")"
# test we found a file - glob might fail
[ -f "$f" ] && cp "$f" /home/docs/log/"${dir}.${file}"
done
done
for f in /usr/share/storage/*/log/myfile.log*; do cp "$f" "$(echo $f | sed -re 's%^/usr/share/storage/([^/]*)/log/myfile(\.log.*)$%/myhome/docs/log/\1\2%')"; done

Bash Script to replicate files

I have 25 files in a directory. I need to amass 25000 files for testing purposes. I thought I could just replicate these files over and over until I get 25000 files. I could manually copy paste 1000 times but that seemed tedious. So I thought I could write a script to do it for me. I tried
cp * .
As a trial but I got an error that said the source and destination file are the same. If I were to automate it how would i do it so that each of the 1000 times the new files are made with unique names?
As discussed in the comments, you can do something like this:
for file in *
do
filename="${file%.*}" # get everything up to last dot
extension="${file##*.}" # get extension (text after last dot)
for i in {00001..10000}
do
cp $file ${filename}${i}${extension}
done
done
The trick for i in {00001..10000} is used to loop from 1 to 10000 having the number with leading zeros.
The ${filename}${i}${extension} is the same as $filename$i$extension but makes more clarity over what is a variable name and what is text. This way, you can also do ${filename}_${i}${extension} to get files like a_23.txt, etc.
In case your current files match a specific pattern, you can always do for file in a* (if they all are on the a + something format).
If you want to keep the extension of the files, you can use this. Assuming, you want to copy all txt-files:
#!/bin/bash
for f in *.txt
do
for i in {1..10000}
do
cp "$f" "${f%.*}_${i}.${f##*.}"
done
done
You could try this:
for file in *; do for i in {1..1000}; do cp $file $file-$i; done; done;
It will append a number to any existing files.
The next script
for file in *.*
do
eval $(sed 's/\(.*\)\.\([^\.]*\)$/base="\1";ext="\2";/' <<< "$file")
for n in {1..1000}
do
echo cp "$file" "$base-$n.$ext"
done
done
will:
take all files with extensions *.*
creates the basename and extension (sed)
in a cycle 1000 times copyes the original file to file-number.extension
it is for DRY-RUN, remove the echo if satisfied

How to remove the extension of a file?

I have a folder that is full of .bak files and some other files also. I need to remove the extension of all .bak files in that folder. How do I make a command which will accept a folder name and then remove the extension of all .bak files in that folder ?
Thanks.
To remove a string from the end of a BASH variable, use the ${var%ending} syntax. It's one of a number of string manipulations available to you in BASH.
Use it like this:
# Run in the same directory as the files
for FILENAME in *.bak; do mv "$FILENAME" "${FILENAME%.bak}"; done
That works nicely as a one-liner, but you could also wrap it as a script to work in an arbitrary directory:
# If we're passed a parameter, cd into that directory. Otherwise, do nothing.
if [ -n "$1" ]; then
cd "$1"
fi
for FILENAME in *.bak; do mv "$FILENAME" "${FILENAME%.bak}"; done
Note that while quoting your variables is almost always a good practice, the for FILENAME in *.bak is still dangerous if any of your filenames might contain spaces. Read David W.'s answer for a more-robust solution, and this document for alternative solutions.
There are several ways to remove file suffixes:
In BASH and Kornshell, you can use the environment variable filtering. Search for ${parameter%word} in the BASH manpage for complete information. Basically, # is a left filter and % is a right filter. You can remember this because # is to the left of %.
If you use a double filter (i.e. ## or %%, you are trying to filter on the biggest match. If you have a single filter (i.e. # or %, you are trying to filter on the smallest match.
What matches is filtered out and you get the rest of the string:
file="this/is/my/file/name.txt"
echo ${file#*/} #Matches is "this/` and will print out "is/my/file/name.txt"
echo ${file##*/} #Matches "this/is/my/file/" and will print out "name.txt"
echo ${file%/*} #Matches "/name.txt" and will print out "/this/is/my/file"
echo ${file%%/*} #Matches "/is/my/file/name.txt" and will print out "this"
Notice this is a glob match and not a regular expression match!. If you want to remove a file suffix:
file_sans_ext=${file%.*}
The .* will match on the period and all characters after it. Since it is a single %, it will match on the smallest glob on the right side of the string. If the filter can't match anything, it the same as your original string.
You can verify a file suffix with something like this:
if [ "${file}" != "${file%.bak}" ]
then
echo "$file is a type '.bak' file"
else
echo "$file is not a type '.bak' file"
fi
Or you could do this:
file_suffix=$(file##*.}
echo "My file is a file '.$file_suffix'"
Note that this will remove the period of the file extension.
Next, we will loop:
find . -name "*.bak" -print0 | while read -d $'\0' file
do
echo "mv '$file' '${file%.bak}'"
done | tee find.out
The find command finds the files you specify. The -print0 separates out the names of the files with a NUL symbol -- which is one of the few characters not allowed in a file name. The -d $\0means that your input separators are NUL symbols. See how nicely thefind -print0andread -d $'\0'` together?
You should almost never use the for file in $(*.bak) method. This will fail if the files have any white space in the name.
Notice that this command doesn't actually move any files. Instead, it produces a find.out file with a list of all the file renames. You should always do something like this when you do commands that operate on massive amounts of files just to be sure everything is fine.
Once you've determined that all the commands in find.out are correct, you can run it like a shell script:
$ bash find.out
rename .bak '' *.bak
(rename is in the util-linux package)
Caveat: there is no error checking:
#!/bin/bash
cd "$1"
for i in *.bak ; do mv -f "$i" "${i%%.bak}" ; done
You can always use the find command to get all the subdirectories
for FILENAME in `find . -name "*.bak"`; do mv --force "$FILENAME" "${FILENAME%.bak}"; done

Linux shell scripts for commands to update multiple folders

I have a script that I created that is just a list of commands (cp's, mkdir's, rm's, etc). What this does is basically update the contents of a bunch of folders with the contents of a source folder.
Right now I have about 15 folders to be modified with about 30 commands each. So when I need to add a folder, I need to add 30 more commands and specify that folder.
Is there a way in the script to create an array of folders to change and loop through it or something?
My script right now just contains basic commands that would normally be run in the command line, so nothing advanced.
Yes, you can do something like this:
for x in "folder1" "folder2" "folder3"; do
mkdir $x
cp foobar $x
done
Better yet, use an array to hold the folder names, e.g.
arr=("folder1" "folder2" "folder3")
for x in ${arr[*]} do
mkdir $x
cp foobar $x
done
If you have specific names following a pattern, you can probably use a loop to generate that list of names automatically.
Here is one approach:
#!/bin/bash
# This function does all you clever stuff
# $1 contains the first parameter, $2 the second and so on
function my_cmds()
{
echo $1
}
# You can loop over folders like this
for i in folder1 folder2 folder3
do
# here we call a function with the string as parameter
my_cmds $i
done
# ... or like this
folders[0]="folder0"
folders[1]="folders1"
folders[2]="folders2"
for i in "${folders[#]}"
do
my_cmds $i
done
a convenient way of initializing an entire array is the
array=( element1 element2 ... elementN )
notation.
This is similar to answers using for loops, but using a here document to store the list of folders. It's like having a data file embedded in the script.
while read -r folder <&3; do
mkdir "$folder"
# etc
done 3<<EOF
folder1
folder2
folder with space
EOF
I use file descriptor 3 for the here document in case you have commands in the body of the loop that attempt to read from standard input.

Resources