Linux - NewbieThis 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
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.
Kick a$$!! GOOD CALL on asking which editor I was using!! I was doing everything via ssh from work earlier, and since I type so friggin slow (well, and I"m just lazy) I was copy/pasting this into NOTEPAD (I know, I know...) and sftp'ing it to the home. What a maroon!
So, re-created these with good old Nedit, and I'm good to go.
The first one resulted in a "Max Dir depth 0" or similar, which I imagine is a simple thing and very fixable. However, the second one worked just fine, though it took roughly 15 seconds to do the 45 pictures in the test directory...thought that would have been quicker...odd...
In any case, you all rock. Thanks for all the replies. I feel kinda bad now for making everybody waste their time typing replies when my silly arse was the only problem, but it's a learning experience for me... ;-)
If that doesn't work, I have no idea what is wrong, the script works fine for me.
You don't have to strip off the shebang line. When executing as "bash script_name" the shebang line becomes what it looks like -- a comment line.
That said, the solution to this seems to be the editor used. Those embedded carriage returns sure mess up a script in such a way as to remain totally invisible when trying to debug it.
primarily this is for converting upper to lower case, but also does other handy formatting like replacing white space with underscores. it's also nicely recursive.
Note: This is a thank you to acid_kewpie as well as something that will go into the Linux questions archives to help others with the same problem.
Wow, that works really good, Kewpie. I have a huge list of excellent 1200x1600 jpg wallpapers that I got from Usenet in alt.binaries.pictures.wallpaper. There is this BookWright dude that posts huge lots of terrific scenery that he gets from bittorrent. Great stuff really, plus maybe an occasional nude if it is sort of classy. Dead Sheep does some of those sometimes. The images are terrific and there are tons of them, all the time. The problem is that the file names are long as hell and filled with spaces and all sorts of nasty things that do not mix well with Linux file systems (Or at least are not easy to work with.) Here are some real file names from the sets:
After the Rain, Tremont Area, Great Smoky Mountains National Park, Tennessee.jpg
Autumn Colors the Tatoosh Range, Mount Rainier National Park, Washington.jpg
Hallett Peak and Lake Haiyaha at Sunrise, Rocky Mountain National Park, Colorado.jpg
Ugh, total nightmare. I wanted something to convert tons of filenames of this sort to something that Linux can tolerate and would be easy to work with. I opened a PuTTY window to my Linux box (Much easier to work in the shell this way than scooting over to the machine and working with the gnome desktop.) and them used vim to create a file called cleanfilename and chmoded the file to 755 to make it executable. I exported my /root/scripts/ directory to my environment and aliased cleanfilename to cfn all from within my /root/.bashrc file, "vim .bashrc" Easy enough to do like so, here is a copy of my root .bashrc file:
# .bashrc
# User specific aliases and functions
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
alias fc3='cd /mnt/old_sys'
alias cfn='/root/scripts/cleanfilename'
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
PATH=$PATH:/root/scripts
I just love making aliases for stuff, so much easier than all of the typing it takes to to repetitious things in the CLI. I just love the CLI as well, can't you tell?
The stuff that is relevant to this message is colored in Red to show what was necessary to change in my .bashrc file to make your script work, in any directory, at any time, with an easy to rememember alias like cfl (Clean File Names)
I did add the:
#!/bin/bash
#
Thing at the top of the file while in vim to show what shell I am using. I did all of my editing in vim, I absolutely love vi and vim especially. That is my shell editor of choice now. I used to use pico but am able to handle vi, it is not that hard to use once you get just a few of the basics down pat, all of the rest of the really cool stuff it can do I google for when I am in need like I was just now. I am actually doing this from Windows XP but in a PuTTY shell, works great and the ANSI colors sure do look pretty in it too. For desktop work, I like to use gedit. I find that very easy to use and both of these editors work just fine with Linux, no surprises like you would expect if you were to edit in a Windows program like notepad or worse, Word, and then copy the file over to Linux. In Windows, I have replaced notepad with Textpad as it is much more powerful than notepad, is very easy to use, and can easily save, open, and edit files in a UNIX format.
Anyway, to use your contraption, I copied all 431 of my wallpaper jpg files over to /usr/share/backgrounds/ and created a directory there called "hodgepodge", chmoded it to make it the same as the other directories in that area, then entered the directory and did an ls to see what I had in there. Ugh, tons of spaces and all sorts of difficult to work with file names. Once in the desired directory, on the command line I typed:
cfn
and hit the enter button. Wowie Zowie, you should have seen that thing go boy! Nice script! Your script is good, it took out all of the nasties and put everything in lower case, and also left the file names as they were so I could find stuff again. Here are the results of the three previous examples after running your cool script:
After the Rain, Tremont Area, Great Smoky Mountains National Park, Tennessee.jpg
thanks thanks thanks. I've been fighting with how to group the filename together while still using variables for an hour now. double quotes it is.
btw- here is the beginning of my script for copying .asc files (the output of our x-ray diffractometer) to .txt files without spaces in the filename for further processing (eventually to a format readable by our peak fitting software).
Quote:
for i in *.asc
do
j=`echo $i|sed -r 's/asc/txt/'`
k=`echo $j|sed 's/ /_/g'`
cp "$i" "$k"
done
To backup and convert all .jpg or .jpeg files in a directory (non-recursively) to .jpg files with underscores instead of spaces:
Quote:
for i in *.jpg *jpeg
do
cp "$i" "$i.bak"
j=`echo $i|sed -r 's/jpe?g/jpg/'`
k=`echo $j|sed 's/ /_/g'`
mv "$i" "$k"
done
Then, you can use rm *.bak to remove the backup files once you make sure the script worked successfully.
EXPLANATION:
Quote:
for i in *.jpg *jpeg #for every file, i, that matches *.jpg and *.jpeg
#in the current directory
do
cp "$i" "$i.bak" # makes a backup copy of each file .bak extension tacked on
j=`echo $i|sed -r 's/jpe?g/jpg/'` # sends inital filename string to sed command, which replaces
#jpg or jpeg with jpg. the -r tells sed to use regular
#expressions, which allow us to use e? to tell it that the e
#is optional.
k=`echo $j|sed 's/ /_/g'` #sends new string, j, to sed, which replaces spaces with underscores
mv "$i" "$k" # moves file with original name , i, to file with newly
#defined name, k.
done
-rv
PS: I'm using Cygwin (A linux shell for Windows) and editing the files in EditPadPro (Windows).
Last edited by ritzvin; 12-27-2007 at 12:21 PM.
Reason: add editor info
Search for files in a directory hierarchy. The default PATH is
the current directory; default EXPRESSION is '-print'
EXPRESSION may consist of:
-follow Dereference symbolic links
-name PATTERN File name (leading directories removed) matches PATTERN
-print Print (default and assumed)
-print0 Delimit output with null characters rather than
newlines
-type X Filetype matches X (where X is one of: f,d,l,b,c,...)
-perm PERMS Permissions match any of (+NNN); all of (-NNN);
or exactly (NNN)
-mtime DAYS Modified time is greater than (+N); less than (-N);
or exactly (N) days
-mmin MINS Modified time is greater than (+N); less than (-N);
or exactly (N) minutes
-exec CMD Execute CMD with all instances of {} replaced by the
files matching EXPRESSION
Max dir depth: -1
Here is the script:
[root@DELPHI scripts]# more spaces_remove.sh
#!/bin/bash
IFS='
'
j=`find $1 -printf "%d\n" | sort -u | tail -n 1`
j=$((j-1)) <--------*** I think this is what is tripping me up ***
echo "Max dir depth:" $j
for (( i=0; i<=j ; i++ ))
do
for name in `find -mindepth $i -maxdepth $i -iname "* *" -printf "%p\n"`
do
newname=`echo "$name" | tr " " "_"`
echo "$name" "$newname"
mv "$name" "$newname"
done
done
##########
Search for files in a directory hierarchy. The default PATH is
the current directory; default EXPRESSION is '-print'
EXPRESSION may consist of:
-follow Dereference symbolic links
-name PATTERN File name (leading directories removed) matches PATTERN
-print Print (default and assumed)
-print0 Delimit output with null characters rather than
newlines
-type X Filetype matches X (where X is one of: f,d,l,b,c,...)
-perm PERMS Permissions match any of (+NNN); all of (-NNN);
or exactly (NNN)
-mtime DAYS Modified time is greater than (+N); less than (-N);
or exactly (N) days
-mmin MINS Modified time is greater than (+N); less than (-N);
or exactly (N) minutes
-exec CMD Execute CMD with all instances of {} replaced by the
files matching EXPRESSION
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.