-
Make "ls" show escape characters
How do I make the output of "ls" show escape characters when listing files. For instance, I have a folder I want to list, when I type "ls" I get this:
Jen's School Pictures/
How can I have "ls" automatically show this instead:
Jen\'s\ School\ Pictures/
I'm trying to include this in my script and I can't seem to get the quoting right, so I thought, why not just have "ls" or some other command quote it for me before I make it part of my script. Thanks
-
I don't think there's really a way to do that, not with ls, at least.
What are you wanting to do with the script? If it's filename matching or something, you don't need to use ls in a shell script. You can just use a wildcard or a directory name. Check out this example. I'm not a hard core scripter, so someone is free to correct any mistakes I've made. Given a list of files:
- foo doo
- foo bar
- foo
- potato
- tomato
You could make a script to show their names, or do something to those files. Let's say we want to
use the touch command on them, just to illustrate:
Code:
for files in * ; do
touch "$files"
done
Now, that uses the touch command, which just changes the modification date to whatever time it was when you touch'ed the file. If you hadn't've used the quotes around "$files", then you would have had a bunch of error messages at "foo doo" and "foo bar", as the shell would have looked at each space to mean another argument for the command. Using the double quotes allows for literal interpretation of the characters inside of the quotes.
So, you wouldn't need to escape the characters if you used quotes. You could just use filename matching and quotes.
Hopefully I'm not wandering aimlessly in this post. I'm not a programmer/scripter, but I do know a few things...
Registered Linux user #230403! Since March 2001! YAY.
Try doing a forum search or a google search before asking a question. And please don't use HELP! in the topic of your post... it's so lame... Please don't PM me for help-- post a question in the forum instead.
-
Re: Make "ls" show escape characters
Originally posted by pwharff
How can I have "ls" automatically show this instead:
Jen\'s\ School\ Pictures/
Hello,
As far as I know, you can't do it in ls. However, I usually use sed, eg.
Code:
ls -1 | sed 's/\ /\\\ /g'
(ls -1 puts each item on a seperate line. 's/\ /\\\ /g' searches for all instances of ' ', and replaces it with '\ '.)
Hope that helps,
Robbo
"Heisenberg may have been here."
-
Thanks for the help guys! Basically what I want to do is delete particular folders in a folder that always has a file in that folder called Library.data
Code:
ls $HOME/Pictures/*/Library.data
Output:
/home/paul/Pictures/Jen's Pictures/Library.data
/home/paul/Pictures/Photo Library/Library.data
/home/paul/Pictures/Test Library/Library.data
ls $HOME/Pictures/*/Library.data | awk -F/ '{ print $5 }'
Output:
Jen's Pictures
Photo Library
Test Library
Now that I know specifically which folders contain the files "Library.data", I want the user to choose which folder (or Library) they want to go to. Once they have made this decision, they then need to choose what album folder they want to delete. The only problem that I am having now is that I try to use the output above (the Library folders), but as you can tell, these include (') and ( ) and could possibly include others in the future. I just don't know how to Quote this so that it will always escape meta-characters such as " ' [ ] ( ) * & ? # ! and so on.
And I could always make the code like this:
Code:
ls $HOME/Pictures/*/Library.data | awk -F/ '{ print $5 }' | sed 's/\ /\\\ /g' | sed 's/\*/\\\*/g' | sed 's/\#/\\\#/g'
But this is very messy and could go on and on and on. There has to be a better way, a way to escape everything at once?
Last edited by pwharff; 10-23-2003 at 11:28 AM.
-
I figured out most of my problem with quoting, I used sed in this way:
Code:
ls $HOME/Pictures/*/Library.data | awk -F/ '{ print $5 }' | sed 's/^/\"/g;s/$/\"/g' > libraries
Now I need to know, how do I output one particular line, like say I just want to output line 2. How do I do this.
Shows the first line, but
Shows the first line and the second. I just want to be able to show just one of the lines, like say line 2 only or line 3 only. And the line that I need to output will change all the time based on how many photo libraries are in the file libraries. So something like this:
Code:
tail -3 libraries | head -1
Will not work for me, it must always show a particular line based on what the users chooses, so if the user chooses 1, just show the first line, if the user chooses 3, just show the third line.
-
why won't that last example work?
couldn't you so something like the following?
Code:
head -$NUM | tail -1
assuming you put the selected number in NUM
maybe that syntax isn't right... i haven't been scripting for a bit... trying to find the right distro and wm right now. (and recover from semi-intentionally killing my entire computer)
-
Perhaps ls has caught up with this old chestnut For the record, ls -b, -Q, --quoting-style=shell or --quoting-style=shell-always (there are other quoting-style options too). As mentioned in an older, sed-based solution, use in conjunction with -1 to get a file-per-line, bare display.
Add the -b switch will escape control characters or spaces as required giving Jen's\ School\ Pictures (i.e. the format needed when passed as an unquoted parameter for example)
-Q quotes it giving simply "Jen's School Pictures" - the correct format between speech marks - but test"me" is rendered as "test\"me\"", correctly escaping the quotation marks (--quoting-style=c has similar results). N.B. it does this to all files, useful when passing as a double-quoted parameter.
--quoting-style=shell single-quotes the name if and where necessary, yielding 'Jen'\''s School Pictures' which I think was the long sought after solution. --quoting-style=shell-always makes it behave like the above, c version and quote all names even if not strictly necessary.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|