Skip to content

Instantly share code, notes, and snippets.

@quachngocxuan
Last active November 30, 2017 14:26
Show Gist options
  • Save quachngocxuan/9602a15f75f3c7a6af4f36b8862cf953 to your computer and use it in GitHub Desktop.
Save quachngocxuan/9602a15f75f3c7a6af4f36b8862cf953 to your computer and use it in GitHub Desktop.
Learn enough to be dangerous - Linux command line

Learn enough to be dangerous - Linux command line

CORE CONCEPTS

Run multiple commands

Using && as connector between commands

Example:

$ git add -A && git commit -m "Add About page"

or

$ git add -A ; git commit -m "Add About page"

The difference is that commands separated by && run only if the previous command succeeded. In contrast, with ; all the commands will be run no matter what, which will cause an error in the likely case that subsequent commands depend on the results of the ones that precede them.

Pipe results

Example piping the result between commands

$ head sonnets.txt | wc

Or piping the result to file

$ ping learnenough.com > learnenough.log

Or append to file

$ ping learnenough.com >> learnenough.log

Hidden files

Hidden files and directories are identified by starting with a dot .

CORE COMMANDS

View manual of a command

$ man <command>

Example:

$ man tail

Check user home directory

$ pwd

Change directory

Change to home directory

$ cd ~

Change to previous directory

$ cd -

List file in a directory

$ ls -l -a

or

$ ls -la

-l option: show by list -a option: show hidden files

List with wildcard

$ ls *.txt

List with long form

$ ls -rtl

View a section in a file

View head of the file

$ head <file>

View end of the file

$ tail <file>

View all file

$ less <file>
  • up & down arrow keys: Move up or down one line
  • spacebar: Move forward one page
  • ⌃F: Move forward one page
  • ⌃B: Move back one page
  • G: Move to end of file
  • 1G: Move to beginning of file
  • /: Search file for string /rose
  • n: Move to next search result
  • N: Move to previous search result
  • q: Quit less

Find a string in the file

Find all occurrences 'time=3.40' in file 'learnenough.log'

grep time=3.40 learnenough.log 

Find string from command line result. Ex: find the process 'spring' in running processes

$ ps aux | grep spring

Find file/direcotry name in file system

$ find . -name '*.txt'

Append a text line in a file

$ echo "hello" >> a.txt

If the file is not existing, it would be created. But be careful, using only one > would clear all existing lines of file:

$ echo "again" > a.txt

Count a file: lines, words, bytes

$ wc <file>

To create new directory with intermediate folders

$ mkdir -p <folder stack>

Example:

$ mkdir -p repos/website

The directory repos is created, and the directory website is also created which is inside repos

Create an empty file - placeholder

$ touch <filename>

Check difference between two files

$ diff <file1> <file2>

List of all running processes

$ ps aux

with sorting

$ top

Kill a process

$ kill <process id>

or kill a process by name

$ pkill -f <process name>

Check a server availability

$ ping <domain/IP>

Clear screen

$ clear

Print history commands

$ history

Save history into a file

$ history > a.txt

To find executable path of a command

$ which <command>

Example:

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