Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

A little survival guide to UNIX command line


Published on

A cheat sheet with basic commands and other tips to UNIX environment

Published in: Technology
  • Login to see the comments

  • Be the first to like this

A little survival guide to UNIX command line

  1. 1. Livre Digital We focus our business, in open source solutions. A little survival guide to command line Livre Digital Team Livre Digital, all rights reseved c 2017. Licence: Creative Commons Attribution - Share ALike 4.0 this is a survival guide to all unix users of the world, specially the new ones; this poster have quick tips to use in your day to day life on linux, osx, *bsd. Top 10 most necessary commands + description example pwd print working directory $ pwd cd change directory $ cd .. ls list files $ ls -a *.txt cp copy files $ cp -rf /dir /opt/subdir mv move files $ mv oldfile newfilename touch creates new file $ touch file1 file2 mkdir creates a directory file $ mkdir -pv /tmp/dir/subdir rm remove files $ rm -f /tmp/file echo echoes a line $ echo ”hello world” > file.txt cat concatenate/print files $ cat /etc/passwd Working with files and directories $ cd go to the user home directory $ cd /etc enter in the directory etc of / $ cd /etc/nginx enter in the directory nginx of /etc $ cd .. comes back one level up to /etc $ ls /tmp/*.txt list all .txt files in /tmp $ ls -a /home/user list all hidden files of /home/user $ cp file1 copyfile copies a single file $ cp file1 file2 /tmp copies multiple files to /tmp $ cp file1 copyfile copies a single file $ cp -r ./dir1/ /tmp copies recursively dir1 to /tmp $ mv oldfile newname renames oldfile to newname $ mv file1 file2 /tmp moves file1 and file2 to /tmp $ rm -f /tmp/share.txt removes share.txt from /tmp dir $ rm -rf /tmp/lz0 removes lz0 subdir from /tmp dir $ head -n 20 file.txt shows first 20 lines of file.txt $ tail file.txt shows last 10 lines of file.txt $ less file.txt shows contents of file.txt pagined Process Management, Modifying, Compressing, Archiving process management $ top interactive display of processes $ ps aux show all users processes of system $ kill -9 10002 kill a process id (PID) number 10002 $ nice -n -10 2553 give a better priority for pid 2553 (de)compression c: create; t: contents; j: bzip; z: gzip x: extract; v: verbose; r: recursive $ tar cvfz /usr /var file.tar.gz compress /usr and /var into file.tar.gz $ tar xfj file.tar.bz2 -C /opt extract file.tar.bz2 into /opt $ zip -r /tmp compress /tmp dir into $ gzip /var/log/messages compress messages file into .gz install source files $ ./configure && make check dependencies, build software $ make install install software for all users The VI / VIM editor The VI editor is probably the most used editor in UNIX systems, it comes with almost any distribution of Linux, *BSD and can be also used in OSX. The editor comes with two main modes: command and insertion. The insertion mode is commonly to all users, you just type something and text typed will be into file, the command mode is accessed by hotkey [ESC] hotkey description h, j, k, l move cursor left, down, top, right respectively 0, $ move cursor to the beginning and end of line respectively u, [ctrl] + r (u)ndo last and redo last change respectively 255G, G go to line 255, go to last line respectively i, o go to (i)nsert mode, insert on next line respectively There are many others commands in VI, some of these starts with colons (:), here are some of the most used in our opinion: command description :w, :wq (w)rite file, (w)rite file and (q)uit file. :q, :q! quit file, or quit file without change anything :!gcc file.c executes gcc to compile file.c into ./a.out binary :r !date read the output of command date and puts on file :set number show line numbers at left :5,20d delete lines 5 - 20 :%s/old/new/g replaces all occurrences of old to new string :help cmd get help about cmd :vimtutor starts the tutorial mode for learning vi Basic Systems and Network Management Commands $ dmesg display system messages from kernel $ fdisk manage disk partitions table $ fsck /dev/sda check and repair filesyste, /dev/sda $ groupadd dev creates dev group (groupdel removes) $ lsmod list all modules loaded by kernel $ mkfs -t ext4 /dev/disk format /dev/disk with ext4 fs $ mkswap /tmp/file setup a swap area for use $ mount /dev/sdc1 /mnt maps device /dev/sdc1 on /mnt $ passwd billy modifies the password of user billy $ shutdown (-r/-h) now reboot, halts the system at specified time $ umount /mnt unload the device mapped on /mnt dir $ useradd joe add user joe to system (userdel removes) $ ping pings a hostname or ip $ ifconfig -a show interface(s) network information $ route -n displays, add and del gateway on network $ iptables -L -n -v display the firewall rules $ traceroute display the nr of hops to reach google $ netstat -nap display (A)LL socket connections $ dig query all DNS records for host $ host get the ip address of host $ whois get the registry records for domain $ tcpdump -i eth0 -vv sniff all packets of eth0 interface $ nc 25 a really powerful tool for network test $ ssh remote connect on console of a network host Distribution Package Management - Search, Installs, Remove Packages Red Hat Linux - YUM and RPM description example yum the yum software package manager update metadata of repository # yum update upgrade all packages # yum upgrade search for vim rpm on repository # yum search vim install dependencies and the package # yum install vim rpm red hat package manager install a rpm package # rpm -ivh file.rpm list all rpm’s installed on system # rpm -qa shows the rpm package relative to file # rpm -qf /usr/bin/ls shows file contents of package # rpm -ql file.rpm remove redhat package # rpm -ev file.rpm Debian GNU/Linux description example apt-get the apt software package manager update metadata of repository # apt-get update upgrade all installed pkgs # apt-get upgrade install dependencies and the package # apt-get install vim apt-cache browse packages on repository show basic information of a pkg # apt-cache show pkgname show dependencies of pkgname # apt-cache depends pkg search for ncurses .deb on repository # apt-cache search ncurses dpkg debian package manager install a deb package # dpkg -i file.deb list all deb’s installed on system # dpkg -l remove a deb’s installed package # dpkg -r file.deb shows the contents of deb package # dpkg -c file.deb UNIX Directory Structure Tree / /bin /dev /etc /etc/passwd /etc/nginx /home /lib /sbin /var /var/log /var/log/nginx /var/run dir description / root directory /bin binary files /dev device and peripheral /etc system config files /home users directory /lib libraries and kernel modules /opt optional softwares /sbin super binary files /usr programs shareable content /var temporary/log files Structured hierarchy directory; Everything is a file and they are case sensitive;; UNIX Permissions $ cd /etc; ls -lhd passwd profile* rc.local resolv* (list passwd, rc.local and all other files starting by prefix ’profile’ and ’resolv’) -rw-r–r– 1 root root 1.9K May 24 18:43 passwd -rw-r–r– 1 root root 761 Oct 22 2014 profile drwxr-xr-x 2 root root 4.0K Feb 10 17:56 profile.d -rwxr-xr-x 1 root admin 306 Feb 10 17:49 rc.local -rw-r–r– 1 root root 935 May 12 23:41 resolv.conf $ ls -l rc.local -rwxr-xr-x 1 root admin 306 Feb 10 17:49 rc.local $ chmod ug+wx rc.local change permission to (u)ser, (g)roup of (w)rite and e(x)ecute $ chmod o-rwx rc.local removes all permissions (read, write, execute) to any other user/group Livre Digital Team A little survival guide to command line