LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 02-27-2018, 04:40 PM   #1
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,137
Blog Entries: 6

Rep: Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826
Some common file management using simple utilities and regex.


Some common file management using simple utilities and regex.
If you are a newbie, here you go.

Remove spaces in filenames
Code:
for file in *; do mv "$file" ${file// /}; done
Replace spaces in filenames with _
Code:
for file in *; do mv "$file" ${file// /_}; done
Remove _ in filenames
Code:
for file in *; do mv "$file" ${file//_/}; done
Remove 1st to 3rd chars in filenames
Code:
for i in *; do mv $i ${i##???}; done
Keep just chars 1-2 5-6 in filenames
Code:
for i in *; do mv $i $(echo $i | cut -c 1-2,5-6); done
Keep just digits in filenames
Code:
for i in *; do mv $i "${i//[!0-9]/}.${i#*.}"; done
Keep just alpha chars in filenames
Code:
for i in *; do mv $i "${i//[!A-Za-z]/}.${i#*.}"; done
Insert _ after 2nd char in filenames
Code:
for i in *; do echo mv "$i" "${i:0:2}_${i:2}"; done
Batch rename filenames from .MP3 to .mp3
Code:
for i in *.MP3; do mv "$i" "${i//MP3/mp3}"; done
Get filenames without extension
Code:
for i in *; do echo ${i%.*}; done
Get just the file extension
Code:
for i in *; do echo ${i#*.}; done
Keep just filename from path
Code:
i="/home/fred/somewhere/myfile.mp3"; echo "${i##*/}"
Keep just filename from url
Code:
i="http://path/to/a/file.mp4"; echo "${i##*/}"
Find 20 largest files/folders from current path
Code:
du -hsx * | sort -rh | head -20
Find 20 largest files in current dir and sub dirs
Code:
find . -type f -print0 | xargs -0 du | sort -n | tail -20 | cut -f2 | xargs -I{} du -sh {}
Find empty files folders
Code:
find . -empty
List all commands present on system by folder.
Code:
ls ${PATH//:/ }
List commands on machine
Code:
apropos --sections 1,8 -w '*' | less
alternate lines from 2 files to make 3rd file
Code:
paste -d "\n" 1st.txt 2nd.txt > 3rd.txt
Stop lines at of text at 65 characters
Code:
fold -sw 65 file1.txt > file2.txt
Find diff of 2 files
Code:
grep -Fxvf file1 file2
diff file1 file2
comm -3 <(sort file1) <(sort file2)
Get file from another machine over ssh
Code:
scp fred@192.168.0.2:/home/fred/file.txt /home/john/
Put file to another machine over ssh
Code:
scp file.txt fred@192.168.0.2:/home/fred/
Find existence and size of remote file over http
Code:
wget --spider url
curl -I url
lynx -head -dump url
HEAD url
w3m -dump_head url
Make a .pdf or .txt from a man page for easier reading
Code:
man -t ls | ps2pdf - output.pdf
man ls | roff2pdf > output.pdf
man -P cat ls > output.txt
man ls | roff2text > output.txt
man ls | col -b > output.txt
man -t ls | ps2ascii - output.txt
And as always, read those man pages.

Anyone else who wishes should add to this list.
 
Old 02-28-2018, 08:57 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,830

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
you forgot the rename command, which will do all the rename/move commands you posted.
 
1 members found this post helpful.
Old 02-28-2018, 04:23 PM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,125

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
I also prefer "basename" - I have a real problem remembering all the special character combinations for bash string manipulation, although they certainly have their place.
 
Old 02-28-2018, 04:41 PM   #4
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,789

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
Please edit your post#1 and put each command argument in "quotes"!
Examples
Code:
for file in *; do mv "$file" "${file//_/}"; done
for i in *; do mv "$i" "${i##???}"; done
for i in *; do mv "$i" "$(echo "$i" | cut -c 1-2,5-6)"; done
In the last example there are two nested commands, echo and mv; both must have their arguments quoted.
 
Old 03-01-2018, 12:20 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,830

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
Quote:
Originally Posted by syg00 View Post
I also prefer "basename" - I have a real problem remembering all the special character combinations for bash string manipulation, although they certainly have their place.
You are right. But:
basename is running in a new shell, therefore it is much more expensive. Even a function with a readable name can be faster. What I could suggest is: just add a comment line before that unreadable command to explain what's happening.
 
  


Reply

Tags
bash



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
How do I find source code for common utilities to compile myself? mxmaniac Linux - Newbie 5 11-16-2015 12:58 AM
Would like to share some common regex patterns... Madhu Desai Linux - Newbie 4 12-11-2013 12:19 AM
LXer: Supercharging package management with yum plugins and utilities LXer Syndicated Linux News 0 11-19-2007 11:50 AM
problems upgrading dapper, common system utilities missing SerfurJ Ubuntu 2 01-28-2007 06:09 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:49 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
Open Source Consulting | Domain Registration