linux cheatsheet


Terminal:

Ctrl-A (^A) -> Move to the beginning of the line
Ctrl-E (^E) -> Move to the end of the line
Ctrl-left arrow -> Move backward a word
Ctrl-right arrow -> Move forward a word
Ctrl-U -> Deletes from cursor to beginning of line
Ctrl-K -> Deletes from cursorr to end of line
Ctrl-Shift-C -> Copy to clipboard
Ctrl-Shift-C -> Paste from clipboard

ls -R my_folder -> lists the contents of my_folder recursively
cd -> takes you to the home directory

cd Exercise\ Files -> escape the space in the directory name
cd “Exercise Files” -> Another approach if the directory name has space

mkdir -p home/legal/contracts -> There is no legal folder inside home. What -p option does is that it creates contract folder plus its parent folder, legal. All this in just one command.

remove files: rm files?.txt -> will remove all files named file + one more character follwed by .txt extension. Meaning, if there is file1.txt or file2.txt, they will be deleted. file.txt will not be deleted.

find: find . -name "pow*" -> to find a file, we need to to give a location from where to start the search and the pattern it should look for. Here, it will start looking for in the current working directory. And it will look for anything that starts with pow .. be it file or folder. * is a wild card character. It matches everything.

find . -name "*ow*" will list out any files or folders with ow in their name (starting from current directory and its subfolders).

find ~/Documents -name "*ow*" will list out any files or folders with ow in their name (starting from Documents directory and its subfolders).

Switch User command: su -> this is used to login as some other user

Most comonly used as sudo -> login temporarily with root privileges.

When you are done using sudo, always do sudo -k to give up those privileges

To actually login as root, do:su root and then to switch back to user, type exit

File Permissions:
user group others the_file
rwx rwx rwx the_file

Change the permissions on a file by modifying the file mode bits.

Two methods to represent permissions:

  • Octal (e.g. 755, 644, and 777)
  • Symbolic (e.g., a=r, g+w, and o-x)
  • Symbolic:
    User(u),Group(g),Others(o),All(a):

    to give user rwx permission => u+rwx

    Note:

  • + : adds permission
  • = : adds permission but removes others. That is, it applies only those values you specify and removes others.
  • – : removes permission from whatever is there
  • if you leave off a prefix, chmod applies permissions to all three: user, group and others. Like chmod +rwx will give read, write and execute permissions to user, group and others (meaning all).

    So, to only give read permission to user: do chmod u=r (since we used user prefix, only user privileges are affected)

    Octal notation is like using = all the time.

    How to run an executable on command-line
    executable is a file that can run on its own. like so ./test.sh here test.sh is an executable and can be run like shown.

    To change ownership of file:
    changing owner of file: like, if I want to change the owner (i.e. user) of a file to root: chown root test.sh will make root the owner of test.sh

    changing owner of group for a file: chgrp command

    Pipes

    Take the output of one command and give it to other. |

    cat user.txt | sort -u | … and so on

    echo "hello" | wc will return 1 1 6. This is actually the result of wc which tells us that there is one line of text, 1 word, and 6 characters. It says 6 instead of 5 because wc is also counting an invisible character called new-line at the end of hello.

    cat

    Its short for concatenate.

    Concatenate means to put two things together, and it can do that but its usually used to print things to screen.

    This command is also useful to get the contents of a file into a series of piped commands.

    head and tail commands show head and tail contents of a file. head -n 5 file.txt will show the first 5 lines of file.txt.

    we can use cat, head and tail through pipes to get some nice things.

    cat file.txt | cat -n | tail -n 1 will output something like this:
    55 last line in file. What happened was that cat -n numbered all the lines of the output from prev command and then tail printed the last line of the output.

    less

    less is also a useful command to check the contents of a large file.

    f -> move forward

    b -> move backward

    enter to move one line at a time.

    space-bar to move one page at a time.

    grep

    Very handy in searching through a file.

    grep -i "the" file.txt will output all lines with word the in them. Case insensitive.

    grep -ni "the" file.txtwill output all lines with word the in them along with the line nos where they occur. Case insensitive.

    grep -v "the" file.txtwill output all lines not having word the in them. Case sensitive.

    Using grep with regular expressions:

    grep -E "[hjkl]" file.txt will display all lines that have h, j,k or l characters in them. -E means regular expressions are used in this grep.

    awk and sed

    a whole topic into themselves

    awk is mainly used in extracting data

    sed is mainly useful in changing data

    tar

    short for taped archive.

    tar usually archives the files but not compresses them.

    .tar.gz means tar archive with gzip compression

    .tar.bz2 means tar archive with bzip compression

    tar -cvf myfile.tar Exercise\ Files/
    here -c means create archive. v stands for verbose. f means save it into a file (it used to be tape drive). myfile.tar is the archived file name. archive Exercise Files folder. In this case its just one folder but you could put more than one folder.

    We can put a option to also compress the files along with archiving. a option tells tar to figure out the compression format based on the file-name extension provided.

    tar -cvaf myfile.tar.gz Exercise\ Files means archive and compress with gzip.
    tar -cvf myfile.tar.bz2 Exercise\ Files means archive and compress with bzip. Sometimes offers slightly better compression but takes longer to run.

    Uncompress/Un-archive
    tar -xf myfiles.tar.bz2 will extract the files. If we don’t want all kinds of files as a result of extraction, make a folder, and do this inside that folder.

    tar -xf myfiles.tar.bz2 -C unpack2 first make folder unpack2 and then run the command. -C mean, change to folder unpack2 for uncompressing. It will put all the contents of uncompression inside folder unpack2

    Redirection

    Name What it is Descriptor
    Standard Input(stdin) Keyboard input 0
    Standard Output(stdout) Text on screen 1
    Standard Error(stderr) Error text on screen 2

    If we have a file list.txt in current directory and we say: ls list.txt, the result is sent to stdout.
    ls list.txt 1>result.txt Shows the descriptor for stdout. We use that to write the result in to result.txt. Since this is such a common thing, we shorten it to
    ls list.txt>result.txt

    > is the redirection operator.

    If however we say ls not_existing_file.txt > result.txt where not_existing_file.txt is a non existing file, we will see that result.txt is empty and the error message still gets printed on the screen. This is because, though the error message is printed on screen, it is actually written to stderr.
    ls not_existing_file.txt 2>result.txt will actully catch that error into result.txt.

    We can have more than one rediection. We can pipe the two together (stderr and stdout) to do some common things like: redirect the list of stdout to a list of successfully copied files and the stderr to a list of files that failed to copy.

    The redirection operator, > ,can be a bit dangerous.for example >result.txt will empty the contents of the file. This is because the redirection operator empties a file before writing it. So if there is nothing on the left side of redirection operator, there file on right side simply gets overwritten with new contents being an empty string.

    Use >> for appending to file.

    for example, echo "appended text" >> file.txt will just add “appended text” (without the quotes) to the end of the contents of file.txt

    System Info

    How much RAM does a system has available: free -h Check under Mem. -h option just prints numbers in human readable numbers.

    Computer Info (processor resources): There is a file in the proc directory called cpuinfo. cat /proc/cpuinfo

    Disk space available and used in the system: df -h look under / which is the root folder. All other folders are managed by the system.

    How much space files and folders take up on your system: du command is for this purpose.

    Lets list out how much space files and folders are taking in my root.

    sudo du / -hd1 Here sudo is to get root access. -h means print in human readable numbers. d1 is the detail level. Here its detail level of , meaning, only show me one level deep, just the first level away from the root. Adding everything up into each of those folders.