Useful Bash commands
This is a list of Bash commands that I frequently use with a short explanation. It may help you in everyday usage of your Unix based system. Tested on OS X but should also work on Ubuntu and others.
Listing files/folders sorted by time
When you need to find most recently edited file/folder in a directory this will be a useful command:
ls -tCommand details:
lsList directory contents-tSort by time modified (most recently modified first) before sorting the operands by lexicographical order
Rename files and folders recursively with find
The command is pretty simple:
find . -name some_name -execdir mv {} other_name \;It will rename all some_name.* files and some_name directories to other_name recursively.
Command details:
findWalks a file hierarchy.Path where to start searching for files-name some_nameName of the files and/or directories which will be renamed-execdir mv {} other_name \;Command to execute for each file found where:mvMove file{}Current file nameother_nameNew file name\;Command termination
Rename multiple files inside folder with rename
For example changing a file extension in the current directory might look like this:
rename 's/.js/.jsx/g' *Command details:
renameRenames multiple files's/.js/.jsx/g'Regexp which will be applied to the name of every file*Path for the modified files e.g.imports/client/components
List sizes of all folders inside a directory
This is very useful if you want to find largest directories:
du -sh *Command details:
duDisplay disk usage statistics-sDisplay a summary for each specified file. (Equivalent to -d 0)-h"Human-readable" output. Use unit suffixes: Byte, Kilobyte, Megabyte, Gigabyte, Terabyte and Petabyte.*Path for the files to list
NOTE: If you want to sort that list by size you may use:
du -sh * | sort -hWhere -h in sort referes to the:
-h --human-numeric-sort compare human readable numbers (e.g., 2K 1G)
On OS X you will have to install coreutils package (it is as easy as typing brew install coreutils). If you don't use Homebrew you should definitely start using it.
For Mac OS X the command will look as follows:
du -sh * | gsort -h