Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
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.
I'm writing a shell script to process some files on an ntfs filesystem drive to get them off and onto an ext3. In the windows spirit, these directories have names like "My Really Cool Dir/", etc. When I write
Code:
for directory in `ls -ad */`; do
...
done
the whitespace is used as a string delimiter, which is no good for my purposes. This is an issue with ls, so
Code:
for directory in `ls -ad */ | sed 's| |\ |g'`; do
isn't fixing it.
I'm sure this is childsplay for someone with more experience! Help?
you're probably better off doing whatever your doing with a find command...
find /var/where/ever -exec rm {} \;
to, in this example, sequentially delete each file found. each file name stays as a known variable within the script, so whitespace never matters.
to stick with ls, try
ls -1ad | while read filename
do
echo file name is $filename
done
as read will read one line at a time, not one word at a time. also you can screw around with the IFS shell variable so for delimits on a newline instead of a space...
Last edited by acid_kewpie; 06-30-2007 at 11:58 AM.
If you have filenames with spaces in them in variables, be sure to put double quotes around the variables when using the variables in a script.
For example:
Code:
for texts in /mnt/windows/Documents\ and\ Settings/jhwilliams/My\ Documents/*.[tT][xX][tT]; do
cp "$file" ~/Documents/"$file"
done
for pdfdocs in /mnt/windows/All\ Users/My\ Documents/*.PDF; do
mv "$file" ~/Documents/"${file// /_}"
done
The Second line replaces spaces in the files with the the underscore character.
If you have two files in a director: "file name1.ext" and "another file.ext", if yo u have a command like
for file in *;
do
# something here
done
The first time in the loop, $file contains "file name1.ext", and not "file\ name1.ext". The "\" characters are used when typing such files in the shell interactively to escape the whitespace, preventing the shell from separating the filename into two arguments. Once you press enter, their job is done. They have already done their job.
The variable IFS (or inter-field separator) in BASH determines what characters act as delimiters. By default space, tab and newline characters are used as delimiters. The variable can also be used to define custom delimiters like comma, colon etc..
After this you can use your directory variable for referring to your windows folders
On the Command line, with a file or a directory named "Split Name", I refer to it as "Split*" and have no trouble. In my experience a file or directory name with white space in it invites trouble sooner or later. I use data file or folder names, not only free from white space but (at least after the first level where defaults like "Pictures" run wild), with entirely lower case characters. I retain folder names like "Pictures" etc with their uppercase starting character because sometimes software is also twisted that way.
For speed and ease using the Command Line, I've found it handy with file or folder names, to try to have a unique filename character or two at the start of the name. Commands like
"cp P*/k*/p*/f*/g* De*" save a lot of typing!
well "Split*" will match "Split Names", "Split Peas" and "Split Mlik"... that's not going to any use in the long run is it?
seems to me you've probably not got as comfortable with bash tab auto completion as would really suit... no need for crude workarounds whenbash will fill in the rest of the file name for you correctly.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.