#!/bin/bash
cd $1
for f in *; do
mv "$f" "${f%.*}.bu"
done
Answer from Maythux on askubuntu.com#!/bin/bash
cd $1
for f in *; do
mv "$f" "${f%.*}.bu"
done
Try that instead (assuming that $1 is a folder):
for f in
f" "$f.bu"
done
Or use the rename utility:
rename -v 's/$/\.bu/' $1/*
Those commands add an extension. If you want to change the existing extension use that:
rename -v 's/\.[^\.]+$/\.bu/' $1/*
You were right to consider rename first. The syntax is a little strange if you're not used to regexes but it's by far the quickest/shortest route once you know what you're doing:
rename 's/\d{4}/2503/' file*
That simply matches the first 4 numbers and swaps them for the ones you specified.
And a test harness (-vn means be verbose but don't do anything) using your filenames:
$ rename 's/\d{4}/2503/' file* -vn
file0901201437404.p renamed as file2503201437404.p
file0901201438761.p renamed as file2503201438761.p
file1003201410069.p renamed as file2503201410069.p
file2602201409853.p renamed as file2503201409853.p
file2602201410180.p renamed as file2503201410180.p
This should do the trick:
for f in file*; do mv
{f/${f:4:8}/25032014}; done
It replaces the string beween the 4th and the 12th character with "25032014".
Copy and rename file from shell script
linux - Shell script to rename files - Stack Overflow
Rename files on Ubuntu Terminal - Stack Overflow
bash - Rename all files in directory from $filename_h to $filename_half? - Stack Overflow
Videos
This is a pretty basic question but I’ve been struggling getting this working for some reason. I am trying to copy a file from one directory to another and renaming it along with the copy. This is being done inside of a shell script, and I have a variable called $filename that stores the NEW file name. Here is the code snippet:
filename="IRcam_fpga_cksm_${checksum}_ver_${version}.pdb"
#filename="This_is_a_file.txt"
echo "filename: ${filename}"
cp ./par/ircam_fpga/designer/impl1/*.pdb output/pl/$filenameThe output of the echo command on the console is:
.pdbname: IRcam_fpga_cksm_A415_ver_0x0081
But the file that gets copied to the new directory does not have the correct name. When I use the version of $filename that is commented out, it works perfectly fine.
why are you using ls with an extra process to while loop? Just use a for loop with shell expansion. This is preferred way
#!/bin/bash
shopt -s nullglob
for file in *
do
if [ -f "$file" ];then
newfile="${file##* }"
mv "$file" $newfile"
fi
done
Postman almost has it. For rename.sh, the awk command returns nothing so in effect you have the following command the shell attempts to execute:
mv rename.sh
hence the error message "missing destination file"
you can fix this by testing for the filename of the script, either hardcoded or $0, and executing the mv command only if $FILE does equal the script name.
you can rename like this
mv "name.txt" "new_name.txt"
you can change the file format like this
mv "name.txt" "new_name.php"
You use command mv filename new_filename
Here is my terminal for your filenames:
marko@DevLaptop:~/test$ touch C6Y5DACXX-2-ID01_1_sequence.fastq
marko@DevLaptop:~/test$ ls
C6Y5DACXX-2-ID01_1_sequence.fastq
marko@DevLaptop:~/test$ mv C6Y5DACXX-2-ID01_1_sequence.fastq C6Y5DACXX-2-ID01_sequence_1.fastq
marko@DevLaptop:~/test$ ls
C6Y5DACXX-2-ID01_sequence_1.fastq
And to replace all 1_sequence to sequence_1 you can use
rename 's/1_sequence/sequence_1/' *.fastq
Escape the space, e.g. Spring\ 2011, or use quotes, e.g. 'Spring 2011'. In the future, it's typically a bad idea to use file names with spaces in them on any *NIX.
If you've got rename, you can use this:
rename ' ' '_' [filenames...]
If you don't have rename or prefer to use just the shell:
for f in *\ *; do mv "$f" "${f// /_}"; done
Broken down:
*\ *selects all files with a space in their name as input for the theforloop. The pattern*X*selects all files withXin their name, and for the special character space, we have to escape it with a slash so that bash doesn't treat it as separating different arguments.- The quotes around
"$f"are important because we know there's a space in the filename and otherwise it would appear as 2+ arguments tomv. ${f//str/new_str}is a bash-specific string substitution feature. All instances ofstrare replaced withnew_str.
A few things to note:
the syntax for command substitution is
$( command )not($ command )the syntax for printing the second field in awk is
print $2notprint=$2you need to pass the name of the file as an argument to awk ex.
awk 'NR==1 {print $2}' "$f"
Also note that the new file will be created relative to the current directory, rather than the subdirectory sequences (there's not enough detail in your question to know whether that's the intended outcome or not).
Thank you steeldriver! The remarks helped a lot! The 3rd one was the real problem solver! So here's the script I wrote in the end:
#! /bin/bash
for f in $(ls); do
mv "$f" "$( awk 'NR==1 {print $2}' "$f" ).pdb";
done
If anyone has any 'smarter' ways of doing it I would be very happy to hear them.
Thanks!
Just use bash, no need to call external commands.
for file in *_h.png
do
mv "$file" "${file/_h.png/_half.png}"
done
Do not add #!/bin/sh
For those that need that one-liner:
for file in *.png; do mv "$file" "${file/_h.png/_half.png}"; done
Try rename command:
rename 's/_h.png/_half.png/' *.png
Update:
example usage:
create some content
$ mkdir /tmp/foo
$ cd /tmp/foo
$ touch one_h.png two_h.png three_h.png
$ ls
one_h.png three_h.png two_h.png
test solution:
$ rename 's/_h.png/_half.png/' *.png
$ ls
one_half.png three_half.png two_half.png