LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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 12-13-2004, 06:25 PM   #16
jeffreybluml
Member
 
Registered: Mar 2004
Location: Minnesota
Distribution: Fedora Core 1, Mandrake 10
Posts: 405

Original Poster
Rep: Reputation: 30

Yup, sure did :-(

But keep the suggestions coming...we have to stumble upon the reason sometime. I just know it's something stupid...
 
Old 12-13-2004, 06:37 PM   #17
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 59
What editor are you using to create the script?
 
Old 12-13-2004, 07:26 PM   #18
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Made it executable, too?
ls -l space_remover


Cheers,
Tink
 
Old 12-13-2004, 09:09 PM   #19
jeffreybluml
Member
 
Registered: Mar 2004
Location: Minnesota
Distribution: Fedora Core 1, Mandrake 10
Posts: 405

Original Poster
Rep: Reputation: 30
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... ;-)

Thanks again, LQ rules.
 
Old 03-02-2007, 07:13 PM   #20
ghostis
LQ Newbie
 
Registered: Mar 2007
Location: Boston
Distribution: CentOS 7, Debian Testing, Ubuntu Server (latest LTS)
Posts: 10

Rep: Reputation: 0
Wink One-liner for this

Here is a one-liner for doing all the files with spaces in the current directory.


Quote:
find . -type f -name "* *" -maxdepth 1| while read src; do mv "$src" `echo $src | tr " " "_"`; done


-Adam
 
Old 03-03-2007, 09:54 AM   #21
Bruce Hill
HCL Maintainer
 
Registered: Jun 2003
Location: McCalla, AL
Distribution: Funtoo
Posts: 6,926

Rep: Reputation: 128Reputation: 128
To remove the spaces in files ->
for i in *; do mv "$i" `echo $i | tr ' ' '_'`; done
 
Old 03-06-2007, 05:11 PM   #22
ghostis
LQ Newbie
 
Registered: Mar 2007
Location: Boston
Distribution: CentOS 7, Debian Testing, Ubuntu Server (latest LTS)
Posts: 10

Rep: Reputation: 0
Quote:
Originally Posted by Bruce Hill
To remove the spaces in files ->
for i in *; do mv "$i" `echo $i | tr ' ' '_'`; done
Won't that zap directories too?

-Adam
 
Old 03-19-2007, 04:14 PM   #23
edmond.nolan
LQ Newbie
 
Registered: Mar 2007
Posts: 1

Rep: Reputation: 0
Hi,

Thanks for the script, it seems it will only replace the first space in a filename so adding the global flag helps:

#!/bin/bash
#
usage()
{
echo "Usage: $0 [directory]"
exit 1;
}

test -d "$1" || usage

dir="$1"

ls $dir | grep -e "[:alnum:]" | \
while read i; do
j=`echo -n "$i" | sed -e 's/ /_/g'`
echo mv "$dir/$i" "$dir/$j"
done
 
Old 03-22-2007, 01:30 AM   #24
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 671Reputation: 671Reputation: 671Reputation: 671Reputation: 671Reputation: 671
This is what I use:

for file in *.jpg; do
mv "$file" "${file// /_}"
done

While you are at it, you can also replace other "evil" characters:

for file in *.jpg; do
mv "$file" "${file//[\[\]\! ]/_}"
done

It's easy enough to remember and type in as needed.
 
Old 03-23-2007, 12:41 PM   #25
wpn146
Member
 
Registered: Jan 2005
Distribution: Solaris, Linux Fedora Core 6
Posts: 170

Rep: Reputation: 30
Quote:
Originally Posted by visaris
Hell, try stripping off the #! line and do:

bash script_name

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.
 
Old 04-08-2007, 02:43 AM   #26
ohmster
Member
 
Registered: May 2005
Location: South Florida
Distribution: CentOS 7
Posts: 39

Rep: Reputation: 1
Thumbs up

-snip-
Quote:
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

after_the_rain,_tremont_area,_great_smoky_mountains_national_park,_tennessee.jpg

Autumn Colors the Tatoosh Range, Mount Rainier National Park, Washington.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

hallett_peak_and_lake_haiyaha_at_sunrise,_rocky_mountain_national_park,_colorado.jpg


Sweet, huh? Great stuff, thank you!
Ohmster
 
Old 12-27-2007, 11:55 AM   #27
ritzvin
LQ Newbie
 
Registered: Dec 2007
Posts: 4

Rep: Reputation: 0
Cool

Quote:
Originally Posted by acid_kewpie View Post
mv -v -- "$OLDNAME" "$TMPNAME"
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
 
Old 05-01-2008, 06:25 PM   #28
snakernetb
LQ Newbie
 
Registered: Dec 2004
Location: Bloomington, IL
Distribution: FC3
Posts: 5

Rep: Reputation: 0
replace script

I am trying to figure out why I am getting this output. I am sure I am missing something easy.

[root@DELPHI scripts]# ./spaces_remove.sh /shares/PUBLIC/Music/*
BusyBox v1.2.2 (2007.06.30-14:33+0000) multi-call binary

Usage: find [PATH...] [EXPRESSION]

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
##########
 
Old 05-01-2008, 09:01 PM   #29
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,357

Rep: Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367
The PATH param to find must be a singleton i.e. no wildcards e.g. '*'. After all, find recurses downwards anyway ...

PS Re your orig request for Perl, here's the full docs, inc tutorials on top left menu.
http://perldoc.perl.org/
See also tutorials/snippets/code sections here: http://www.perlmonks.org

Last edited by chrism01; 05-01-2008 at 09:04 PM.
 
Old 05-02-2008, 07:29 AM   #30
ghostis
LQ Newbie
 
Registered: Mar 2007
Location: Boston
Distribution: CentOS 7, Debian Testing, Ubuntu Server (latest LTS)
Posts: 10

Rep: Reputation: 0
Quote:
Originally Posted by snakernetb View Post
I am trying to figure out why I am getting this output. I am sure I am missing something easy.

[root@DELPHI scripts]# ./spaces_remove.sh /shares/PUBLIC/Music/*
BusyBox v1.2.2 (2007.06.30-14:33+0000) multi-call binary

Usage: find [PATH...] [EXPRESSION]

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`
<SNIP>
##########
The issue is that busybox find seems not to have the printf flag. Try swapping "-print" for

-print0 |

instead of

-printf "%d\n" |


.

-Adam

Last edited by ghostis; 05-02-2008 at 07:31 AM.
 
  


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
Bash script to remove capitalisation and spaces form a filename scuzzman Programming 11 05-18-2008 12:28 PM
script to change multiple filenames in a directory jeffreybluml Linux - Newbie 8 12-06-2006 01:46 AM
need help with script to remove all metachars from filenames BrianK Programming 5 08-20-2005 11:10 PM
Changing multiple filenames with a script brecki Linux - Newbie 8 01-30-2004 03:10 PM
spaces in filenames ebone Linux - General 2 11-12-2001 11:56 AM


All times are GMT -5. The time now is 07:51 PM.

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