56.2 F
San Francisco
Sunday, September 13, 2020

Removing Spaces Linux/Mac

bash

I had a bunch of files I needed to upload to a web server.  The filenames contained spaces and we know some web browsers don’t like spaces.  There was this script I found on the web.  It worked out really well.

#!/bin/bash

ls > /tmp/list
# we have to make the list file in a separate
# directory so that it doesn’t get included
# in the list of the current directory!
mv /tmp/list ./list
cat list | tr ‘ ‘ ‘_’ > listnew
# this turns spaces into underscores
FILE_COUNT=$(wc -l list | awk ‘{print $1}’)
LOOP=1
while [ “$LOOP” -le “$FILE_COUNT” ]
do
AWKFEED=”FNR==”$LOOP
# the command to feed awk
# output will be ‘FNR==1’, ‘FNR==2’, ..
OLDFILE=$(awk $AWKFEED list)
NEWFILE=$(awk $AWKFEED listnew)
mv “$OLDFILE” “$NEWFILE”
# keep the quotes to make the shell read file
# names with spaces as one file
LOOP=$(($LOOP+1))
done

rm list
rm listnew

exit 0

To get this to work, create a file in the same directory. Change the mode to make sure the file is executable.

References:
http://www.dba-oracle.com/t_fix_linux_data_file_names_special_characters.htm

Edel Alonhttp://edelalon.com
Edel-Ryan Alon is a starving musician, failed artist, connoisseur of fine foods, aspiring entrepreneur, husband, father of two, geek by day, cook by night, and an all around great guy.

Related Articles

S2000 Glove Box Organizer

Just highlighting this glove box organizer for the S2000 made by Modify Products. They seem to have other items for sale that fit this...

Today’s Winner

Just finished a lobster feast at New England Lobster in Burlingame, California. Many of you read about the 15-pound lobster. It was not a...

How to Check Your Air Quality in the San Francisco Bay Area

The Bay Area Air Quality Management District (BAAQMD) has hourly air quality reports. Check your specific area for air quality before going out. Soot,...

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Stay Connected

440FollowersFollow
9,970FollowersFollow
12SubscribersSubscribe
- Advertisement -

Latest Articles

Today’s Winner

Just finished a lobster feast at New England Lobster in Burlingame, California. Many of you read about the 15-pound lobster. It was not a...

How to Check Your Air Quality in the San Francisco Bay Area

The Bay Area Air Quality Management District (BAAQMD) has hourly air quality reports. Check your specific area for air quality before going out. Soot,...

Truffle Shiso Poke

Just experimenting...but I love it when an experiment turns into something great. This poke had a bright citrus woody flavor. Ingredients 1 pound of tuna 1/4 onion 2...

Yeti Rambler Review

I was only able to survive this past heatwave with my new Yeti Rambler. The Yeti Rambler kept my liquids cold. The best feature? Every...

Nikwax

Nikwax has been my choice of cleaning my backpack for over 10 years. Most of the other cleaners out there seem to change the...