scripting: how to cp a list of files which starts with - or space without - or space?
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.
scripting: how to cp a list of files which starts with - or space without - or space?
I have a list of files in a directory which starts with - or have space.
I need to copy them back without the space or -?
Anyone can help me, urgently?
linux1[sim]% ls
-01_SUMMARY_REPORT_ETS030531_10142009.txt -842150451_ETS171528_10012009.log
linux1[sim]% cp ./-* ./*
cp: copying multiple files, but last argument `./-842150451_ETS171528_10012009.log' is not a directory
I have a list of files in a directory which starts with - or have space.
I need to copy them back without the space or -?
Anyone can help me, urgently?
linux1[sim]% ls
-01_SUMMARY_REPORT_ETS030531_10142009.txt -842150451_ETS171528_10012009.log
linux1[sim]% cp ./-* ./*
cp: copying multiple files, but last argument `./-842150451_ETS171528_10012009.log' is not a directory
So the file names begin with either a space or a dash, yes? And you want to strip the space or the dash, yes?
Code:
find /path -type f | while read from
do
to="$from"
to=${to/-}
to=${to/ }
echo "from: \"$from\" to: \"$to\""
# mv "$from" "$to"
done
Don't uncomment the commented line until you are satisfied that the script does what you want.
thanks i tried but there are few syntax error. Can you explain the syntax? Im new to scripting, i dont understand esp at "sed" and "find /path" function.
Syntax errors?
Did you exclude the $'s and >'s? They are only there for clarity, your shell should create them for you.
Code:
$ ls -1 | while read filename # -> ls the directory returning only filenames
> do # -> start the loop
> newfilename=$(echo ${filename} | sed -e 's/ /_/g' -e 's/^-//') # -> use sed to substitute spaces for under-scores and any dash at the beginning of the file is removed
> mv -- "${filename}" "${newfilename}" # -> rename the file
> done # -> end the loop
I tested this on Linux & Bash and it worked for me.
The 'sed' part of this command has 2 steps:
's/ /_/g' --> the 's' means it's a substitution. the / /_/ means substitute a space for an under-score. the 'g' means globally ie. multiple times on one filename if required.
's/^-//' --> another 's' for substitution. the '^' matches the very beginning of the line, so '/^-//' means substitute a '-' a the beginning of the line for nothing ('//') ie. remove it.
Last edited by rizhun; 10-15-2009 at 01:46 AM.
Reason: added 'sed' regex explanation
thanks i tried but there are few syntax error. Can you explain the syntax? Im new to scripting, i dont understand esp at "sed" and "find /path" function.
Thanks
If a drill sergeant said "I ... state your name ...", would you say your name, or would you say "state your name"? It's the same here -- replace "/path" with the path you personally want to search.
Quote:
Originally Posted by simransab
thanks i tried but there are few syntax error.
Could you possibly be more specific? Imagine being aboard a gigantic airliner and the stewardess says, "Don't worry about that burning wing, folks, the pilot has called the control tower and announced that there are a few syntax errors." Would you worry about your future?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.