LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 04-06-2010, 07:27 PM   #1
idlehands
Member
 
Registered: Mar 2010
Distribution: zLinux, RHEL, Ubuntu, SUSE
Posts: 50

Rep: Reputation: 16
cli help with filenames that have spaces


When I try to do fun cli stuff to move files around etc, I have issues when the file has a space or strange characters in it. Take the following for exampe:


Code:
 cp -r `ls -trbQ |tail -15 |grep tmp -v` tmp
I would expect this to move the last 15 files into a directory i just create called tmp.. however i get:
cp: cannot stat `"Violent': No such file or directory

A work around is to ls -trbQ |tail 15 >file
and then run a while loop around to wrap each line in the file with the cp command for example, and then run the file as a script

Is there some flag i'm missing on ls that will return a filename on the cli that has the correct special characters so cp can find it?

Thanks!
I'm only using cp as an example there are several cases where I want to find the last few files/directories and do "stuff" to them. They always seem to have wonky filenames.
 
Old 04-06-2010, 07:36 PM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
The first rule is: "Don't put spaces or strange characters in file names."

The second rule is: "Rename your files to get rid of the spaces and strange characters."

The third rule is: Do what you have to with quotes and other tricks---because you didn't follow rules 1 and 2.....

Seriously----Rule 3 is completely feasible, but 1 and 2 are MUCH better.
 
Old 04-06-2010, 07:37 PM   #3
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670
Instead of using backticks, you can use xargs.
Code:
ls -trbQ | tail -15 | grep tmp -v | tr '\n' '\0' | xargs -0 cp --target-directory tmp
The `tr' command is used to convert new lines to null characters. The `-0' option of xargs uses null characters to separate arguments. Often it is used in combination with the `find' commands `-print0' argument.

For some complex commands, I will at times use find's -printf argument to print out bash commands with the filenames quoted. Then I will inspect the generated script and run it.

Last edited by jschiwal; 04-06-2010 at 07:40 PM.
 
Old 04-07-2010, 06:47 AM   #4
idlehands
Member
 
Registered: Mar 2010
Distribution: zLinux, RHEL, Ubuntu, SUSE
Posts: 50

Original Poster
Rep: Reputation: 16
I agree with rule 1 and rule 2, though for reasons out of my control its completely unfeasible.

thanks for the tr, will check into it.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
spaces in filenames dotancohen Programming 12 02-17-2006 03:42 PM
filenames with spaces antony.booth Programming 5 11-01-2005 05:49 AM
How to use foreach on filenames with spaces? BrianK Linux - General 3 08-09-2005 01:15 PM
ls and filenames with spaces rose_bud4201 Programming 10 07-01-2005 09:28 AM
spaces in filenames ebone Linux - General 2 11-12-2001 12:56 PM


All times are GMT -5. The time now is 02:34 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration