Skip to content

Instantly share code, notes, and snippets.

@JadedEvan
Last active December 12, 2019 06:41
Show Gist options
  • Save JadedEvan/1251602 to your computer and use it in GitHub Desktop.
Save JadedEvan/1251602 to your computer and use it in GitHub Desktop.
UNIX Tips - a catalog of useful UNIX commands

find commands

find . -regextype posix-egrep -regex ".*(rb|js)$"

Find all files ending in .rb or .js.

find . -name "*js" -o -name "*rb"

Same as above, not using regular expressions.

find . -regextype posix-ergep -regex ".*(rb|js)$" -exec grep -l matchNameHere {} \;

Find all files ending in .rb or .js, then search those files for 'matchNameHere'. The -l option prints the line number of the occurance. (http://www.devdaily.com/unix/edu/examples/find.shtml)

find ~/some/directory -name "*rb" -exec basename {} \;

Find all files ending in .rb, then print a list of filenames (no paths). Note that the ';' is mandatory, it tells find where to stop the command execution

find . -type f -newermt "Aug 10" ! -newermt "Aug 17" -exec rm {} \;

Find all files newer than August 10th, not newer than August 17th and remove them

sed commands

ls *.rb | awk '{print("mv "$1" "$1)}' | sed 's/\.rb/_service\.rb/2' | /bin/sh

Batch rename all files matching a pattern, insert the _service before the file suffix

find . -name "*erb" | awk '{print($1" "$1)}' | sed  's/\.erb/\.haml/2' | xargs -L 1 -t html2haml $1

Find all files ending in erb, replace the extension with haml and then execute the conversion via html2haml with xargs

  • -L 1 tells xargs to listen to one line at a time
  • -t tells xargs to echo the command that it is executing

Fart

find /some/path -name "*rb" -o -name "*yml" | xargs grep -sl "some_phrase" | xargs sed -i -e 's/some_phrase/replacement_phrase'

Find all files ending in *rb or *yml, then grep those files for some_phrase, then replace those instances using sed

cat /some/file.txt | awk '{print "mkdir /path/to/"$0""}' | /bin/sh

Read input from file, pipe to awk to generate a system command, then execute it.

<filename.json python -mjson.tool

Pretty format a JSON file

User Operations

useradd {username}

Create a new user

chage -d 1 {username}

Force username to change their password upon the next login

usermod -a -G {groupname} {username}

Adds the user {username} to group {groupname}

Bash shell tips

Command Substitution

$ cat foo.txt
$ ^foo^bar^
=> cat bar.txt
$ aws s3api copy foo.txt --remote foo.txt
$ !!:gs/foo/bar
=> aws s3api copy bar.txt --remote bar.txt

Using the caret ^ operator performs a substitution on the last command that was run. This is useful if you misspell something or need to iterate over some redundant scripts. In the command above, foo.txt is replaced using the caret with bar.txt

Using the !!:gs substitution will replace all instances of the line.

Repeat command

Repeat the last run command by using two exclamation points

$ cat foo.txt
$ !!
=> cat foo.txt

Useful for when you need to execute sudo commands

$ cat foo.txt
Permission denied
$ sudo !!
  • $? outputs the exit status of the last command
  • $_ run the last command

Miscellaneous

Return a UNIX timestamp for a custom date on OS X:

date -j -f "%Y-%m-%d %T %z" "2014-06-13 14:35:10 +0700" "+%s"

Reveal certain lines in a file:

sed -n '100,200p' filename.txt

Show biggest files in a directory

du -k | sort -nr | head -n 10

GPG

Sign a document

"Signing" a document uses the private key to encrypt it. Anyone with the public key can thus decrypt it. GPG works in reverse by default - uses the public key to encrypt and private key to decrypt. Default usage requires that parties exchange public keys.

To sign a document

gpg --sign file.txt

To decrypt the document

gpg --import public.key
gpg --output file.txt --decrypt file.txt.gpg

See this for more info:

gpg --export -a "User Name" > public.key

export a public key into file public.key. This will create a file called public.key with the ascii representation of the public key for User Name.

gpg --export -a "User Name"

Prints out the public key for User Name to the command line, which is only semi-useful

gpg --export-secret-key -a "User Name" > private.key

Export a private key. This will create a file called private.key with the ascii representation of the private key for User Name.

gpg --import public.key

Import a public key. This adds the public key in the file "public.key" to your public key ring.

gpg --allow-secret-key-import --import private.key

Import a private key. This adds the private key in the file "private.key" to your private key ring. There's a note (*) at the bottom explaining why you may want to do this.

RedHat / Debian

rpm -qli <package-name>

Show installation path for a package

Set the system date

sudo date MMDDHHmmYYYY
sudo date 103109092014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment