A piggy bank of commands, fixes, succinct reviews, some mini articles and technical opinions from a (mostly) Perl developer.

Jump to

Quick reference

Showing posts with label spaces. Show all posts
Showing posts with label spaces. Show all posts

Linux renaming examples, when filenames have spaces

Do your filenames have spaces? Do you curse bash for making it difficult to loop over them?

Fear not! (and wash that potty mouth out with soap). This loop is all you need:

find . -type f -name "*.mp3" -print0 | while IFS= read -r -d '' file; do ls "$file"; done

# Rename "foo.mp3.mp3" to "foo.mp3"

find . -type f -name "*.mp3" -print0 | while IFS= read -r -d '' file; do file=`basename "$file"`; newfile=$(perl -le'$ARGV[0]=~s/\.mp3//;print$ARGV[0]' "$file"); echo mv "$file" "$newfile"; done


# Add a leading zero to numbered filenames

find . -type f -name "064*" -print0 | while IFS= read -r -d '' file; do file=`basename "$file"`; echo mv "$file" "0$file"; done

# The above commands will print out what they're about to do. Check it is what you expect! Then make this change to run them for real:

Change: echo mv foo bar
To: `mv foo bar`

# ^ Those ` are backticks

# Note the order of files is not sorted


Bash: How to loop through a list of terms containing spaces

Change the Internal Field Separator (IFS):

O=$IFS
IFS=$(echo -en "\n\b")
for f in $(ls *)
do
echo "$f"
done
IFS=$O

Type a literal tab in vi

If you've got "convert tabs to spaces" turned on, type this to insert a real tab:
Ctrl-V [tab]

Vim 'unset' command

Instead of an "unset" command, vi prefixes the option with "no".

So to turn off visible tabs and newlines, use:

:set nolist

Vim: Turn tabs into spaces

:set tabstop=4

:set expandtab