LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 09-02-2016, 06:04 PM   #1
dannyboy34
LQ Newbie
 
Registered: Sep 2016
Posts: 4

Rep: Reputation: Disabled
witespaces cause command to fail, when in backquotes


How to work with files|dirs with spaces> I'vs tried the following command below but it fails:

> ls -ald `find . -type d ! -wholename "*.svn*" -name "* *" -printf ""%p"\n"`

I am familiar with the -exec ls option of the find utility.
The goal rather of this post is to understand why the command fails when directories with spaces are found.
By my reasoning, the backquoted `...` output should have all find results encapsulated with quotes so all should be fine,
Example:

> find . -type d ! -wholename "*.svn*" -name "* *" -printf ""%p"\n"
> "./dir1/dir2/dir3 with spaces"
> "./dir4/dir5/dir6 with spaces"
>
> ls -ald `find . -type d ! -wholename "*.svn*" -name "* *" -printf ""%p"\n"`
> ls: cannot access "./dir1/dir2/dir3: No such file or directory
> ls: cannot access with: No such file or directory
> ls: cannot access spaces": No such file or directory
> ls: cannot access "./dir4/dir5/dir6: No such file or directory
> ls: cannot access with: No such file or directory
> ls: cannot access spaces": No such file or directory

BTW:
> ls -ald `find . -type d -name "* *" ! -wholename "*.svn*" -print0`
fails also

Thanks
Danny
 
Old 09-02-2016, 06:16 PM   #2
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Use quotes
Code:
ls -ald "`find . -type d ! -wholename "*.svn*" -name "* *" -printf ""%p"\n"`"
But there are surely other simple ways to achieve what you want to do

Last edited by keefaz; 09-02-2016 at 06:18 PM.
 
Old 09-02-2016, 07:03 PM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Quotes will not work in this case as all the directories found will be joined as one single item, which of course does not exist.

If this has to be done, I would suggest using the -exec option or xargs
 
Old 09-02-2016, 07:06 PM   #4
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,774

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
Quote:
Originally Posted by dannyboy34 View Post
> ls -ald `find . -type d ! -wholename "*.svn*" -name "* *" -printf ""%p"\n"`

By my reasoning, the backquoted `...` output should have all find results encapsulated with quotes so all should be fine,
None of those quote marks are escaped, so they are all being processed by the shell prior to execution of find. If you use "set -x" first, you will see what is actually being passed
Code:
set -x
find . -type d ! -wholename "*.svn*" -name "* *" -printf ""%p"\n"
+ find . -type d '!' -wholename '*.svn*' -name '* *' -printf '%p\n'
./dir1/dir2/dir3 with spaces
./dir4/dir5/dir6 with spaces
Escaping those literal quotes only looks like it should work
Code:
find . -type d ! -wholename "*.svn*" -name "* *" -printf "\"%p\"\n"
+ find . -type d '!' -wholename '*.svn*' -name '* *' -printf '"%p"\n'
"./dir1/dir2/dir3 with spaces"
"./dir4/dir5/dir6 with spaces"
The problem there with command substitution is that the quoting for the outer command line has already been done, so those returned quotes are (a) not going to affect word splitting, and (b) are going to get passed literally to the ls command:
Code:
ls -ald `find . -type d ! -wholename "*.svn*" -name "* *" -printf "\"%p\"\n"`
++ find . -type d '!' -wholename '*.svn*' -name '* *' -printf '"%p"\n'
+ ls --color=auto -ald '"./dir1/dir2/dir3' with 'spaces"' '"./dir4/dir5/dir6' with 'spaces"'
ls: cannot access "./dir1/dir2/dir3: No such file or directory
ls: cannot access with: No such file or directory
ls: cannot access spaces": No such file or directory
ls: cannot access "./dir4/dir5/dir6: No such file or directory
ls: cannot access with: No such file or directory
ls: cannot access spaces": No such file or directory
The closest (I didn't say "best". As you are aware, using "-exec" would be better.) reasonably clean way to do that is to use a NUL separator and "xargs -0":
Code:
find . -type d ! -wholename "*.svn*" -name "* *" -printf "%p\0" | xargs -0 ls -ald
Quote:
Originally Posted by keefaz View Post
Use quotes
Code:
ls -ald "`find . -type d ! -wholename "*.svn*" -name "* *" -printf ""%p"\n"`"
That's just going to pass the entire list (including the internal newlines) as a single, quoted argument to ls, which would work fine if exactly one directory is found. Fails otherwise.
 
Old 09-03-2016, 03:56 AM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,349

Rep: Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750
You might find the -print0 option to the 'find' cmd useful:
http://linux.die.net/man/1/find
https://superuser.com/questions/1186...rgs-and-print0
 
Old 09-03-2016, 02:24 PM   #6
Shadow_7
Senior Member
 
Registered: Feb 2003
Distribution: debian
Posts: 4,137
Blog Entries: 1

Rep: Reputation: 874Reputation: 874Reputation: 874Reputation: 874Reputation: 874Reputation: 874Reputation: 874
The shell interpreter likely strips of the ""'s before interpretation. So using -- can help overcome that or doing escape-age or at least escape the middle space *\ *, or "(backslash)"* *(backslash)"" type tricks. Mostly the -- thing, or using $() instead of `` sometimes helps, although mostly for nested commands.

forum strips some of the \'s.
 
  


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
[SOLVED] linux find command fail with four '-name' MrUmunhum Linux - Software 4 11-02-2014 05:59 PM
Webmin & Wine Command Fail Ra'Jiska Linux - Software 2 04-19-2013 06:09 AM
[SOLVED] Problem with rsync and filenames with spaces, backquotes chongman99 Linux - General 3 10-18-2011 10:49 AM
[SOLVED] Perl backquotes on Fedora-13 problem password636 Programming 5 09-09-2010 07:32 AM
make fail with: gcc-3.4 command not found andy101 Ubuntu 3 11-27-2005 01:45 AM

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

All times are GMT -5. The time now is 04:59 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
Open Source Consulting | Domain Registration