Skip to content

Instantly share code, notes, and snippets.

@soifou
Last active September 21, 2016 14:12
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 soifou/5ea19c74a6a114cf6125 to your computer and use it in GitHub Desktop.
Save soifou/5ea19c74a6a114cf6125 to your computer and use it in GitHub Desktop.
Command-line snippets collection

Command-line collection

Miscellaneous (and messy) useful snippets to use in command-line.

Files/Folders manipulations

  • Output a specific word in a file For example, /path/phpfile contains something like this:
...
define('DB_PASSW', 'password');
...

You want to print the value of DB_PASSW constant.

cat /path/phpfile | grep DB_PASS | awk -F' '{print $4}'

We choose here the field separator ' in -F' and print the 4th occurence.

  • Quickly rename a file
$ mv file.{png,jpg}
  • Create a quick back-up copy of a file
$ cp file.txt{,.bak}
  • Replace word in file(s)
$ sed -i -e "s/old-word/new-word/g" filename
$ find src -type f -exec sed -i 's/old-word/new-word/' {} +
$ grep -rlw 'old-word' * | xargs -i@ sed -i 's/old-word/new-word/g' @
$ find . -type f -print0 | xargs -0 sed -i 's/old-word/new-word/g'
$ find . -name "*.twig" -print | xargs sed -i 's/old-word/new-word/g'
$ find /etc/php5/cli/conf.d/ -name "*.ini" -exec sed -i -re 's/^(\s*)#(.*)/\1;\2/g' {} \;

Last one basically finds all .ini files below /etc/php5/cli/conf.d/ and executes sed on it, which replaces any #comment line literal by ;comment.

  • Change your Git repository domain for all of your project in one shot
find */.git -name "config" -print | xargs sed -i 's/bitbucket.org:oldname/bitbucket.org:newname/g'
  • Prefix all files with "x-files"
$ rename 's/^/x-files-/' *
old> `01.01.90.pilot.avi`
new> `x-files-01.01.90.pilot.avi`
  • Delete all files in directory except one
$ find . \! -name '.htaccess' -delete
  • Zip individually files in a directory
$ for i in *; do zip -r "${i%/}.zip" "$i"; done
  • Encode video to another format
# apt-get install libav-tools
$ avconv -i input.mkv -codec copy output.avi
  • Convert an XLS to CSV with LibreOffice
$ libreoffice --headless --convert-to csv ../dir/file.xls --outdir ../dir

It will create a file.csv in the output parent directory dir/

  • Cut & concat big files into pieces
$ split -b 2000m movie.mkv mymovie (give mymovieaa mymovieab mymovieac)
$ cat mymoviea? > movie.mkv

Images manipulations

# apt-get install imagemagick
  • Compress all JPG in the current path
$ mogrify -path . -quality 85 *.jpg
  • Resize and compress all PNG recursively to max width 250 pixels
$ find . -type f -name "*.png" -print0 | xargs -0 mogrify -resize '250x>' -quality 90

Sysadmin

  • List drive capacity
# df -h  
# fdisk -l
  • Determine where are big files
# du -h --max-depth=3 /* > mybigfiles.txt
  • Find files in current directory created 3 hours ago
$ find . -maxdepth 1 -type f -cmin -180
  • Calculate the size of all files found by find
$ find . -iname "*.png" -print0 | xargs -0 du -ch | tail -1
  • Create a user with sudo privilege, set a password
# useradd -s /bin/zsh -m -d /home/user -c "My Name" -g sudo user
# passwd user
  • Execute a command at a given time
$ echo "ls -l" | at midnight
  • Download entire FTP directory
$ wget -r -l 0 --user="username" --password="pass" ftp://server/directory

Note: -r is limited to a recursion depth of 5 by default. Adding -l 0 disable the recursion depth.

  • Activate cron log
# vim /etc/rsyslog.d/50-default.conf

Uncomment cron.* /var/log/cron.log

# service rsyslog restart
  • List hidden files only
$ ls -ld .*
  • Count all sub-folder of the current folder
$ find . -mindepth 1 -maxdepth 100 -type d | wc -l
  • Cound files in a folder (and its subfolders) recursively
find /folder -maxdepth 1 -type d | while read -r dir; do printf "%s:\t" "$dir"; find "$dir" -type f | wc -l; done
  • Getting external IP Address
$ curl ipecho.net/plain; echo
  • List of commands you use most often
$ history | awk '{print $2}' | sort | uniq -c | sort -rn | head
  • Execute a command without saving it in the history
$ <space>command
  • Change to the previous working directory
$ cd -
  • Jump to a directory. Execute a command. Jump back to current directory
$ (cd /tmp && ls)
  • Recursively remove all empty directories
$ find . -type d -empty -delete
  • Create simple text file from command line
$ cat > file.txt
{your text here}
{your text here}
<ctrl-d>
  • Empty a file
$ > file.txt
  • Display the top ten running processes. (Sorted by memory usage)
$ ps aux | sort -nk +4 | tail
  • Displays a calendar
$ cal 3 1983
  • Output command result in log file AND terminal
$ ls 2>&1 | tee output.log

Misc

  • Zoom desktop in KDE4 using XBindKeys
# apt-get install xbindkeys
$ xbindkeys --defaults > $HOME/.xbindkeysrc
$ vim ~/.xbindkeysrc 

Copy/Paste below

# Zoom in
"qdbus org.kde.kglobalaccel /component/kwin invokeShortcut "view_zoom_in""
  m:0x50 + c:133 + b:4

# Zoom out
"qdbus org.kde.kglobalaccel /component/kwin invokeShortcut "view_zoom_out""
  m:0x50 + c:133 + b:5

(Re)start xbindkeys to apply the new shortcut:

$ killall xbindkeys && xbindkeys

Now you can fully enjoy the zoom effect and add xbindkeys into Autostart kde systemsettings tab.

  • How to encrypt or decrypt a file with OpenSSL To encrypt with aes-256-cbc cipher:
$ openssl enc -aes-256-cbc -e -in <input_file> -out <output_file>

To decrypt an encrypted file:

$ openssl enc -aes-256-cbc -d -in <input_file> -out <output_file>

To list all available cipher algorithms:

$ openssl list-cipher-algorithms

Check website ssl certificate

$ openssl s_client -connect www.example.com:443
  • How to rate limit apt-get when it's downloading packages over network:
$ apt-get -o Acquire::http::Dl-Limit=50 install mysql-server

In this example, apt-get will use 50kB/s bandwidth. Bandwidth rate limit can apply to other actions (install, upgrade, dist-upgrade) as well.

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