Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I need to trim(delete) the first 2,3,4,5,6 character's from a set of file's (2 from some 3 from some etc...)
I run debian is there a easy way to do this as am useless are command's and really don't want to download all them file's and use file-monkey just to reupload them.
Thank's
Last edited by Vodkaholic1983; 02-23-2011 at 04:29 PM.
I presumed only the first line needed alteration rather than each line - the OP can clarify.
That is indeed one way of reading it, now that I look at it more closely. OP does need to clarify, the cut command won't work if only the first line needs to be targeted. The sed command needs a small addition:
I need to trim(delete) the first 2,3,4,5,6 character's from a set of file's (2 from some 3 from some etc...)
Your request is just peculiar enough that I'm going to speculate that what you you really mean is that you want to trim the characters from the file names and not from the file contents. Can you confirm that this is correct or not. It would be a shame to see you try a script that wrecks that many files when you wanted to simply rename them.
#!/bin/bash
find $PWD -type d | grep -v "^$PWD$" | \
while read THISDIR
do
cd "$THISDIR"
ls | while read FILE
do
mv "${FILE}" "${FILE/#[0-9]* - /}"
done
done
Sample run:
Code:
# Before:
~/Temp $ ls -l *
-rwxr-x--- 1 druuna internet 172 Feb 24 12:56 changer.sh
Dir 1:
total 0
-rw-r----- 1 druuna internet 0 Feb 24 12:57 1 - file01.png
-rw-r----- 1 druuna internet 0 Feb 24 12:57 2 - file02.png
Dir 2:
total 0
-rw-r----- 1 druuna internet 0 Feb 24 12:57 11 - file11.png
-rw-r----- 1 druuna internet 0 Feb 24 12:57 12 - file12.png
Dir 3:
total 0
-rw-r----- 1 druuna internet 0 Feb 24 12:57 111 - file21.png
-rw-r----- 1 druuna internet 0 Feb 24 12:57 112 - file22.png
# run the script:
~/Temp $ ./changer.sh
# After:
~/Temp $ ls -l *
-rwxr-x--- 1 druuna internet 172 Feb 24 12:56 changer.sh
Dir 1:
total 0
-rw-r----- 1 druuna internet 0 Feb 24 12:57 file01.png
-rw-r----- 1 druuna internet 0 Feb 24 12:57 file02.png
Dir 2:
total 0
-rw-r----- 1 druuna internet 0 Feb 24 12:57 file11.png
-rw-r----- 1 druuna internet 0 Feb 24 12:57 file12.png
Dir 3:
total 0
-rw-r----- 1 druuna internet 0 Feb 24 12:57 file21.png
-rw-r----- 1 druuna internet 0 Feb 24 12:57 file22.png
Make sure you test this first to make sure thgis is what you actually want!
This does not check for possible duplicate files in a directory, dupes will be overwritten (ie 123 - file.abc.png and 321 - file.abc.png will be reduced to one file: file.abc.png).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.