LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 10-14-2009, 03:01 AM   #1
simransab
LQ Newbie
 
Registered: Sep 2009
Posts: 23

Rep: Reputation: 15
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
 
Old 10-14-2009, 03:32 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Try something like this (not tested)
Code:
ls -1 | while read old_fn
do
    new_fn="${old_fn//-/}"
    new_fn="${new_fn// /}"
    echo "DEBUG: old_fn: '$old_fn'"
    echo "DEBUG: new_fn: '$new_fn'"
done
EDIT: corrected new_fn="${old_fn// /}" to new_fn="${new_fn// /}"

Last edited by catkin; 10-14-2009 at 03:34 AM. Reason: Typo!
 
Old 10-14-2009, 03:35 AM   #3
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by simransab View Post
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.
 
Old 10-14-2009, 03:35 AM   #4
rizhun
Member
 
Registered: Jun 2005
Location: England
Distribution: Ubuntu, SLES, AIX
Posts: 268

Rep: Reputation: 47
Hi simransab,

Try this:

Code:
$ cd /to/directory/with/files
$ ls -1 | while read filename
> do
>    newfilename=$(echo ${filename} | sed -e 's/ /_/g' -e 's/^-//')
>    mv -- "${filename}" "${newfilename}"
> done
I did a quick test and it worked for me in Bash.

Good luck
 
Old 10-14-2009, 07:31 PM   #5
simransab
LQ Newbie
 
Registered: Sep 2009
Posts: 23

Original Poster
Rep: Reputation: 15
hi lutusp and rizhun,

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
 
Old 10-15-2009, 01:42 AM   #6
rizhun
Member
 
Registered: Jun 2005
Location: England
Distribution: Ubuntu, SLES, AIX
Posts: 268

Rep: Reputation: 47
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
 
Old 10-15-2009, 01:55 AM   #7
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
Quote:
Originally Posted by catkin View Post
Try something like this (not tested)
Code:
ls -1 | while read old_fn
do
    new_fn="${old_fn//-/}"
    new_fn="${new_fn// /}"
    echo "DEBUG: old_fn: '$old_fn'"
    echo "DEBUG: new_fn: '$new_fn'"
done
EDIT: corrected new_fn="${old_fn// /}" to new_fn="${new_fn// /}"
Why you shouldn't parse the output of ls

Either use find (preferably with -print0) or bash builtins.

Last edited by i92guboj; 10-15-2009 at 01:56 AM.
 
Old 10-15-2009, 02:36 AM   #8
rizhun
Member
 
Registered: Jun 2005
Location: England
Distribution: Ubuntu, SLES, AIX
Posts: 268

Rep: Reputation: 47
Quote:
Originally Posted by i92guboj View Post
Why you shouldn't parse the output of ls

Either use find (preferably with -print0) or bash builtins.
Ok...

Code:
$ cd /to/directory/with/files
$ find . -type f | while read filename
> do
>    newfilename=$(echo ${filename} | sed -e 's/ /_/g' -e 's/^-//')
>    mv -- "${filename}" "${newfilename}"
> done
Be careful though, this will traverse down the file-system structure.
 
Old 10-15-2009, 04:12 AM   #9
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by simransab View Post
hi lutusp and rizhun,

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 View Post
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?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Do we have any chance of calling user space callback function from kernel space? ravishankar.g Linux - Newbie 1 09-22-2009 07:14 PM
how to call socket prog code written in user space from kernel space???HELP kurt2 Programming 2 07-15-2009 09:56 PM
LXer: Worms in space: NASA confirms International Space Station infected LXer Syndicated Linux News 0 08-28-2008 09:40 AM
To list files consuming disk space ZAMO Linux - General 4 05-05-2008 06:33 AM
List Amount of Space Taken Up by Each Directory? halfpower Linux - General 7 02-26-2007 08:51 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:28 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration