Removing first 3 characters of file names in linux
Use the cut
command:
newName=$(echo "$i" | cut -c4-)
In bash
you can use a Parameter Expansion extension:
newName=${i:3}
Also, don't forget to quote your variables:
mv "$i" "$newName"
Otherwise it will think you're trying to move the files named AB
, file
, and 1.pdf
to a directory named 1.pdf
.
You could also install the rename
command if you don't already have it:
rename 's/^...//' *.pdf
How to remove first 16 characters of all file names in a directory?
In bash, you can run
for f in *; do mv "$f" "${f:16}"; done
to rename all files stripping off the first 16 characters of the name.
You can change the *
to a more restrictive pattern such as *.fits
if you don't want to rename all files in the current directory. The quotes around the parameters to mv
are necessary if any filenames contain whitespace.
bash's ${var:pos:len}
syntax also supports more advanced usage than the above. You can take only the first five characters with ${f::5}
, or the first five characters after removing the first 16 characters with ${f:16:5}
. Many other variable substitution expressions are available in bash; see a reference such as TLDP's Bash Parameter Substitution for more information.
What is a unix command for deleting the first N characters of a line?
Use cut
. Eg. to strip the first 4 characters of each line (i.e. start on the 5th char):
tail -f logfile | grep org.springframework | cut -c 5-
Delete the first five characters on any line of a text file in Linux with sed
sed 's/^.....//'
means
replace ("s", substitute) beginning-of-line then 5 characters (".") with nothing.
There are more compact or flexible ways to write this using sed or cut.
How to remove the first 2 letters of multiple file names in linux shell?
You can use the syntax ${file:2}
to refer to the name starting from the 3rd char.
Hence, you may do:
for file in F*png
do
mv "$file" "${file:2}"
done
In case ${file:2}
did not work to you (neither rename
), you can also use sed
or cut
:
for file in F*png
do
new_file=$(sed 's/^..//' <<< "$file") <---- cuts first two chars
new_file=$(cut -c3- <<< "$file") <---- the same
mv "$file" "$new_file"
done
Test
$ file="Ff6_01.png"
$ touch $file
$ ls
Ff6_01.png
$ mv "$file" "${file:2}"
$ ls
6_01.png
Removing part of a filename for multiple files on Linux
First of all use 'sed -e' instead of '\e'
And I would suggest you do it this way in bash
for filename in *.fasta; do
[ -f "$filename" ] || continue
mv "$filename" "${filename//test.extra/}"
done
SED : Remove last four characters from filename
if your file names don't have spaces, you can: (under your test_images
dir)
ls -1|sed -r 's/(.*)....(\.jpg)$/mv & \1\2/'
to check the generated mv
command. If it is ok, add |sh
to the above command to do the actual renaming.
If your filenames have spaces, you need add quotes:
..../mv "&" "\1\2"/'|sh
This is a quick and dirty solution, since working with ls result is not good practice.
update: add "how to" example:
LsyHP 11:41:40 /tmp/test/img
kent$ ll
total 0
drwxr-xr-x 2 kent kent 120 Jan 4 11:41 ./
drwxr-xr-x 3 kent kent 160 Jan 4 11:41 ../
-rw-r--r-- 1 kent kent 0 Jan 4 11:41 006103insettedryshampoo-blossom7096.jpg
-rw-r--r-- 1 kent kent 0 Jan 4 11:41 008299bathmassagesponges-3packa1a4.jpg
-rw-r--r-- 1 kent kent 0 Jan 4 11:41 008507colgatetripleactiontoothpaste125d.jpg
-rw-r--r-- 1 kent kent 0 Jan 4 11:41 8729teatreeoilantisepticcream25g1005.jpg
LsyHP 11:41:43 /tmp/test/img
kent$ ls -1|sed -r 's/(.*)....(\.jpg)$/mv & \1\2/'
mv 006103insettedryshampoo-blossom7096.jpg 006103insettedryshampoo-blossom.jpg
mv 008299bathmassagesponges-3packa1a4.jpg 008299bathmassagesponges-3pack.jpg
mv 008507colgatetripleactiontoothpaste125d.jpg 008507colgatetripleactiontoothpaste.jpg
mv 8729teatreeoilantisepticcream25g1005.jpg 8729teatreeoilantisepticcream25g.jpg
LsyHP 11:41:52 /tmp/test/img
kent$ ls -1|sed -r 's/(.*)....(\.jpg)$/mv & \1\2/'|sh
LsyHP 11:41:57 /tmp/test/img
kent$ ll
total 0
drwxr-xr-x 2 kent kent 120 Jan 4 11:41 ./
drwxr-xr-x 3 kent kent 160 Jan 4 11:41 ../
-rw-r--r-- 1 kent kent 0 Jan 4 11:41 006103insettedryshampoo-blossom.jpg
-rw-r--r-- 1 kent kent 0 Jan 4 11:41 008299bathmassagesponges-3pack.jpg
-rw-r--r-- 1 kent kent 0 Jan 4 11:41 008507colgatetripleactiontoothpaste.jpg
-rw-r--r-- 1 kent kent 0 Jan 4 11:41 8729teatreeoilantisepticcream25g.jpg
Related Topics
Why Proc Process Faster Than Others
How to Read a Value from User Input into a Variable
What Is How to Call Execve with Arguments in Assembly
A Wrong Size of "Len" Calculated by $ - Symbol with Fasm Equ
How to Install Vsftpd on Centos 6
Difference Between Double and Single Bigger Than in Linux Terminal
Mongodb Service Fails After Reboot
Chef-Server Stuck Ruby_Block[Supervise_Rabbitmq_Sleep] Action Run on Docker Container
How to Remove Space/Tab from Command Output
How to Set Just Year with Linux Date Command
How to Run Cassandra (Cqlsh) from Anywhere
How Long Does It Take for a Non-Blocked Signal Get Delivered
Running a Program Through Ssh Fails with "Error Opening Terminal: Unknown."