code everywhere

helpful terminal commands

posted on June 22, 2014, 7:53 pm in terminal

Working as a programmer, your should be using terminal commands at some point in your day.

Here's a list of some useful terminal commands to speed up your work.

The Basics

Lets recap the basics that everyone should be familiar with.

bash -- 70x32
mkdir NAME
..
cd NAME
..
touch file.txt
..
echo "some text..." > file.txt
..
ls -lah
..
cat file.txt
..
cp path/from path/to
..
rm -rf path/to/file.txt
..
clear
Make a new directory with mkdir, switch to directories with cd or cd .. to move to the parent directory.

Use touch to create a new file or update a file's date/time.

And echo "some text..." > file.txt to write data to that file.

Use ls to list files, add the -lah to get hidden files and file sizes in a human format.

Use cat to get the contents of a file, for large files your better off using tail.

Copy a file with cp, adding from location and to location.

Delete a file with rm -rf, add the flags to do it forcefully and recursively.

To much info, clear the screen with clear. OSX users can use CMD+K, works also.

Tail

bash -- 70x32
tail -n 100 path/to/file.txt
tail -f path/to/file.txt
To get partial file contents, use tail. With the -n 100 flag you'll get the last 100 lines. But files like logs are constantly changing, use tail -f to see that log file live.

Delete a process

bash -- 70x32
ps aux
kill -9 PID
Find out what processes are running using ps aux. You can then pipe the output into grep to filter it, example ps aux | grep node.

Once you have the PID, use kill -9 to stop that process immediately. If your wondering why -9, stackoverflow.com/a/9951578 .

The Magic of GREP

Grep is useful for filtering output to only get what you need. It is an incredible powerful tool.

bash -- 70x32
ps aux | grep node
ls -l | grep Oct
...
cmd | grep WHAT
For basic usage you can grep TEXT or use the -A 10 -B 10 to get the lines before and/or after.

With grep, you can also filter thru entire directories using grep -r 'text_goes_here' path_goes_here to find that one file your looking for.

Need to exclude files? Use --exclude={*.zip,*.txt}.

Or try grep -rnw . -e 'text_to_find to find files.

Read the manual (gnu.org/software/grep/manual/grep.html ) for all the details.

Compressing Files (.zip

/ .tar)
bash -- 70x32
zip -r package.zip . -x "*.DS_Store"

tar cfv filename.tar *
The command above will zip a directory (or you can specify only certain files). The -r will do it recursively and the -x will ignore .ds_store files (for Mac users).

tar is similar when zip is not installed.

directory loops

With a for loop, you can apply a command to a list files.

bash -- 70x32
for i in *; do echo "cmd ${i}"; done;

Replace the do echo "cmd ${i}" with anything you want.

Connection snitch

bash -- 70x32
lsof -i | grep -E "(LISTEN|ESTABLISHED)"
Display a list of active connection and see what your computer is doing on the internet.

Networking Help

bash -- 70x32
ping remoteserver
ifconfig
The ping command is useful to check if your network connection is working. The ifconfig gives you your network details.

Aliases in zsh

bash -- 70x32
vi .zshrc
...
alias ll='ls -lsha'

printfn() { echo $1 }

And if you can't remember something, use history to get past commands and !LINE to recall one.

recent posts

< back
find something helpful? send us some coin BTC 1A6KbS5AmYQVAh5Zcy9rVSnMAoCEunLUqT

(ad) Need Hosting? Try Linode, VPS Starting from $5

privacy policy