Skip to content

Instantly share code, notes, and snippets.

@nickautomatic
Last active September 27, 2019 15:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nickautomatic/7822903 to your computer and use it in GitHub Desktop.
Save nickautomatic/7822903 to your computer and use it in GitHub Desktop.
Useful Linux commands
ack --sass "([\d\.]*)r?em" --output="\$1" -h | sort -u
Find all unique em and rem values in all Sass files below the current directory
cat / less
Output contents of a text file. ("less" is paginated but disappears after end of file, "cat" outputs in one go but output remains visible in console).
chown user:group /something
Changes owner of /something to 'user', group to 'group'.
cp
This copies the directory /home/jane, with all its files and subdirectories, and creates a subdirectory named jane in the current directory (.) :
cp -r /home/jane .
/dev/null
a black hole which unwanted output can be written to to silence a command.
eg. eval `ssh-agent -s` &> /dev/null
df -h
Show how much disk space is free
du -hs [directory]
Display summary of a directory's disk usage in human-readable form
du -ch -d 1 | sort -hr
Show a summary of all sub-directories' file sizes, sorted from largest to smallest
find
Recursively search directory and subdirectories for files that match a pattern, eg:
find . -name "*.ext"
git --git-dir=../somerepo/.git format-patch -k -1 --stdout 03ace01 | git am -3 -k
Cherry-pick a single commit (with hash 03ace01) from somerepo
grep 'something' *.php
Find all occurrences of 'something' in all the php files in the current directory
grep -c "." [filename]
Echo number of lines in file (if replacing "." with a regex, echo number of matches in file)
lame
Batch encode all wavs in current directory:
for f in *.wav; do lame -b320 $f; done
Rename all files from .wav.mp3 to .mp3 and move them to ../mp3/
for f in *.mp3 ; do AUFILE=`echo "$f" | sed 's/.wav//g'` ; mv -f "$f" "../mp3/$AUFILE"; done &
(nb. mv -f forces overwrite of identically-named files without prompting)
less a.txt
view contents of a.txt
less a.txt | grep http*
display all hyperlinks in a.txt
ln -s a b
Create a symbolic link "b" that links to file "a"
ls -alFhv --color
Display colour-coded verbose directory listing with human-readable file sizes (show all, and append / to directories)
and ordered in proper numeric rather than alphabetical order (ie. listing 1000 after 999)
ls -l | wc -l
Display number of files in a directory (ie. return line count from listing directory)
mkdir -p path/of/some/depth
Create directory and all its parents, if they also don't yet exist (without the -p flag, mkdir complains if /path/of/some doesn't exist)
mkdir -p {dir1,dir2,dir3/subdir}
Create multiple directories simultaneously. The above would create dir1/ dir2/ and dir3/subdir/
The curly bracket thing is called "shell expansion". You could also mkdir topdir/{dir1,dir2}/{subdir1,subdir2}, which would create topdir/dir1 and topdir/dir2, each of which would have subdir1 and subdir2 inside them.
rm -r dir
Delete directory "dir", all its contents, and all its sub-directories
scp username@remote.host:/path/to/file localfile
Copy file from remote host to "localfile"
sudo /etc/init.d/apache2 restart
sudo apachectl graceful
Restarts Apache (two alternatives)
tail -n
list last n lines of file (default = 10)
type
alternative to `which` that also works for aliases
eg. `type gs` --> "gs is aliased to `git status'"
&
putting & at the end of a command runs it in the background and returns you to the command line to get on with other stuff.
>
send output of previous command to a file, eg:
ls -R > dirlist.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment