Thursday, August 5, 2010

Linux - Commands

Linux


ToDo:
  • Add more tips.

Terminal Commands:
  • wc -l filename.ext  -  Count the number of lines (-l), words (-w) ou letters (-c) of a file.
  • sudo ufw status  - Status to Firewall
  • alias  -  It works as an alias for a command, regular expression and/or instruction. Example:  alias ll='ls -l'
  • Mounting File Systems:
    • mount /dev/fd /floppy =Mount a file system.
      • exFAT:
        • sudo mount -t exfat /dev/sdc1 /media/exfat
      • NTFS:
        • sudo mount -t ntfs -o nls=utf8,umask=0222 /dev/sdb1 /media/windows
    • umount /floppy = Unmount a file system.
  • Finding files:
    • find /home -name "file*" 2> /dev/null = Search for files, ignoring access errors.
    • find /home -iname "file*" 2> /dev/null = Search for files, case insensitive, ignoring access errors.
  • which <app-name-or-command> = Prints the full path to shell commands
  • test -f <filename> = Test if a file exist.
  • scp -r ~/Desktop/test  <user>@<ip_address_of_user>:/home/user/Desktop  =  Copying an entire directory.
  • cp <source> <destination> = Copy files and/or folders:
    • -r  or -R = recursive copy
    • -p = preserves the following characteristics of each source file in the corresponding destination file: data modification,  last access, ownership, and the file permission bits. 
    • -f = force copy. Also, ignore errors if the file/folder does not exist.
  • rsync -vap --ignore-existing <source_file> <destination_file> = copy files and directories to a destination, similar to the cp command. However, it also allows copying to remote locations and can provide a progress bar.
    • Key flags:
      • v = verbose
      • r = recursive
      • p = preserve permissions
      • g = group
      • o = owner
      • a = archive
      • --progress = progresss bar
  • cat file1.txt file2.txt > files1and2merged.txt = Creates a new file by merging files 1 and 2.
  • cat file1.txt >> file2.txt = Add file1 at the end of file2.
  • > <file-name> = Clear the file without deleting it.
  • touch <filename.ext> = Create an empty file named <filename.ext>(0Kb).
  • ln -s source_file symbolic_link =  Create a symbolic link to a given file.
  • mkdir -p /folder/subfolder  =  Create directory trees with a single command.
  • CURL:
    • curl http://some.url --output some.file = download the file and save it.
    • curl -IL mail.com = Test an HTTP 301 redirect
  • grep -i -r -n search4 * = Search for words "search4" within text files.
  • whereis eclipse = Search for installed application.
  • echo "Line 1"  >  ./test.txt = Creates a file with the content "Line 1". If the file exists it will be overwritten.
  • echo "Line 2"  >>  ./test.txt = Appends the content at the end of the file. If the file does not exist it will be created.
  • rm -r * = Removes directories and files simultaneously.
  • rm -f file_* = Force remove files
  • find . -name "*.ptz" -delete = Delete a huge number of files.
  • pwd  =  Prints the current directory to the screen.
  • printenv = List environment variables.
  • Managing Users and Groups:
    • passwd = Change user's password
    • passwd -d  <user> =  remove the password for a user
    • getent passwd =  List the users
    • whoami = List the current user.
    • who =  List all logged users.
    • w = List all logged users.
    • sudo adduser -m <username> = Creates a new user called 'git'. (-m) creates the user's folder
    • usermod -aG username sudo <username> = add user 'username' to SUDOERS
    • usermod --shell /bin/bash <username> = Change the login shell for the specified user.
    • chsh -s /bin/bash = Change your login shell.
    • echo $SHELL = The shell for the current user but not necessarily the shell that is running at the moment
    • usermod -aG <username> <group-name> = adds user 'username' to group 'group-name'
    • sudo deluser --remove-home <username> = remove the `git` user and its folder (e.g: /home/git)
    • groupdel group-name = Remove the group "group-name".
    • gpasswd -d group-name username = Remove a user from the group.
    • groups user-name = List groups of the user
    • id -nG = List groups of the current user
    • groups = List all groups
    • getent group = List all groups and its users
    • getent group <group-name> = List group info
    • cut -d : -f 1 /etc/passwd = List all users of Linux
    • cat /etc/passwd = List all users of Linux
    • echo username:newpassword | chpasswd = Change the user's password without asking for any input
  • mkpasswd -l 8 =  Generates a complex random password at the specified length.
  • wall "<my-message>" = Send a message to everyone connected to the machine.
  • uname -a = List kernel version, including whether it is 32 or 64 bit
  • iwconfig = List wireless devices
  • iwlist =  Lists additional information about the wifi network interface, not listed by the iwconfig command
  • ifconfig = List the configured network devices
  • ip a -c =  List network devices
  • ifconfig -a = List the configured network devices, regardless they are configured or not.
  • sudo ifconfig wlan0 up = Activate the 'wlan0' network adapter. 
  • sudo ifconfig wlan0 down = Deactivate the 'wlan0' network adapter. 
  • Managing used space on disk per folder:
    • du -sh ./* = Find out the weight of a directory or file with the specified mask (./*)
    • sudo du -h --max-depth=1 | sort -hr = finding out which directories are using all your space
    • du -h -s = List the summarized disk space used by a folder.
  • Free up disk space:
    • sudo journalctl --vacuum-time=3d
    • sudo apt-get autoremove
    • sudo apt-get clean - This will remove the content of the /var/cache/apt/archives directory.
    • sudo apt-get autoclean - Unlike clean, autoclean only removes the packages that are not possible to download from the repositories.
    • sudo du -sh /var/cache/apt
    • du -h /var/lib/snapd/snaps
    • du -sh ~/.cache/thumbnails
  • List the 10 largest files:
    • sudo find . -type f -exec ls -l '{}' \; | awk '{print $5, $NF}' | sort -n | tail -10
    • sudo find . -printf '%s  %p\n' | sort -nr | head -10
  • List the 10 largest folders:
    • du -h --max-depth=1 | sort -rh | head -n 10
    • du -h --max-depth=1 /path/to/directory | sort -rh | head -n 10
  • pidof svnserve = Show the PID of the process "svnserve", if it's running.
  • ps aux = List all running processes in Linux
  • kill -9 <process-id> = sends a SIGKILL signal indicating to a service to shut down immediately. An unresponsive program will ignore a kill command, but it will shut down whenever a kill -9 command is issued. Use this command with caution. It bypasses the standard shutdown routine so any unsaved data will be lost.
  • sudo = Run commands as a root user (See details)
  • sudo su - = Will put you into a root environment, but it will ask you for your user password instead of the root password.
  • cat ~/.bash_history = Listing of commands executed in the terminal (by user).
  • history -f = Show the date and time in the history command output in zsh shell.
  • history -c = Delete the command log history file of the current user.
  • history -d <history-id> = remove the line number <history-id> from history
  • history -a = Saves each command executed in the terminal during the current session to the file ~/.bash_history. Force your shell to append every command entered during a current shell session into ~/.bash_history file.
  • history -r = Read the commands saved in the file ~/.bash_history and update the list in memory. If you want your history to be updated and to reflect changes in your ~/.bash_history file, you can read all history commands from the ~/.bash_history file.
  • To force it to automatically store every command into a ~/.bash_history file immediately after command execution you can take a look at HERE.
  • cat /dev/null > ~/.bash_history && history -c && exit  = Delete the log of executed commands, including the list saved in the memory\.
  • runuser -l <username> -c '<command>' = Run command as another user.
  • Running multiple commands in one line:
    • A ; B       # Run A and then B, regardless of the success of A
    • A && B  # Run B if and only if A succeeded
    • A || B       # Run B if and only if A failed
    • A &         # Run A in the background.
  • yes | <command> = For commands or scripts that require an interactive response.
  • 32-bits or 64-bits:
    • lscpu =  Show if the Linux kernel is 32 bits or 64 bits. (CPU op-modes).
    • file /usr/bin/file = Show if the Linux kernel is 32 bits or 64 bits.
    • arch = Show if the Linux kernel is 32 bits or 64 bits.
    • pdkg --print-architecture = Show if the Linux kernel is 32 bits or 64 bits.
    • getconf LONG_BIT = Show if the Linux kernel is 32 bits or 64 bits.
  • Ubuntu Desktop ou Server:
    • dpkg -l ubuntu-desktop   =  Returns "dpkg-query: no packages found matching ubuntu-desktop", if using Ubuntu Server.
  • Hostname:
    • hostname -I =  List all IPs of the machine.
    • hostnamectl  =  List the computer host name
    • Mudar o nome do host:
      • sudo hostnamectl set-hostname newNameHere
      • sudo nano /etc/hosts
        • Replace any occurrence of the existing computer name with your new one
      • sudo reboot
  • How to detect the OS from a Bash script:
    • echo $OSTYPE
    • echo `uname`
    • platform=$(uname) && echo $platform
  • SSH:
    • sudo apt install openssh-server -y  =  Install ssh server
    • ssh-copy-id -i <id_rsa.pub> <remote-user>@<remote-host-ip-or-name>  =  Copy the public ssh-key to the remote computer, then you can log in to this <remote-host-ip-or-name> server without a password from this particular system.
  • Public IP: We can get the public IP address of a machine using the Linux bash terminal by making a request to a web service that returns the IP address of the machine making the request. Here are a few examples using different web services: 
    • curl ifconfig.me
    • curl api.ipify.org
    • curl ipecho.net/plain
  • df = reports file system disk space usage.
    • df - h
    • df -Th = include the file system type on a particular disk partition
  • lsblk = List the block information of connected devices and their mappings
  • sudo fdisk -l  ~= lsblk
  • lsusb = List USB devices
  • lspci = List device types and models found by the system
  • lshw = Hardware configuration info
  • lsmod = List loaded modules (eg. bluetooth, ip_tables, network drivers).
  • lscpu = Show CPU information
  • lsmem = List the ranges of available memory with their online status
  • lsof = Identifies which files are in use (opened) by which processes. To see the files opened in a specific directory, then use:
    • lsof +D /path/to/folder
  • strace -p PID = traces system calls and signals. It provides a detailed look at what a process is doing.
  • cat /proc/meminfo = Find ram size
  • watch <command> = Periodically running that <command> at a certain set interval of time (2s default). 
  • wpa_passphrase <ssid> [passphrase] = returns encrypted [passphrase] for network <ssid>.
  • Find out the Linux distribution and version:
    • uname -a
    • lsb_release -a
    • cat /etc/os-release
    • cat /etc/*release
    • hostnamectl
  • diff -u <(ls -l /directory/) <(ls -l /directory/) | colordiff  = Comparing output of two ls commands
  • nohup <command> = Keep your command running in the background, even if you accidentally close the terminal window.
  • screen = You can connect to a remote machine, start a screen session, and launch a process. You can disconnect from the remote host, reconnect, and your process will still be running. You can share a screen session between two different SSH connections so two people can see the same thing, in real-time.
    • screen = Start a screen session.
    • screen -ls = Get a list of the detached sessions
    • screen -r <screen-session-id> = Attach to session <screen-session-id>
  • echo $BASH_VERSION = Show bash shell version
  • netstat -ltpn = Display network socket related information.
    • -l = to print all listening sockets
    • -t = shows all TCP connections
    • -u = displays all UDP connections
    • -p = enables printing of application/program name listening on the port
    • -n = To print numeric values rather than service names
  • nslookup google.com = Get information about servers on the internet or your local network. It queries DNS to find the name server information and can be useful for network debugging.
  • Display Routing Table
    • netstat -rn
    • sudo route -n
    • ip route list
  • ss -nlt = Display network socket related information on a Linux system.
    • -l - display listening sockets only
    • -t - display all TCP connection
    • -n - do not try to resolve service names
  • amixer sset 'Master' 50% - Increase the volume.
  • <Ctrl><r> - search the user's command history.
  • cd - = return to the previous folder.
  • acpi -i -b = reports the battery charge status.
  • upower -i /org/freedesktop/UPower/devices/battery_BAT0  - Check the laptop battery status.
  • date -s "19 APR 2012 11:14:00" -   Update date and time.
  • sudo timedatectl set-ntp off    sudo timedatectl set-ntp on - Update date and time online.
  • sudo add-apt-repository  ppa:PPA_REPOSITORY_NAME/PPA - Add a PPA Repository
  • sudo add-apt-repository --remove ppa:PPA_REPOSITORY_NAME/PPA - Remove a PPA Repository
  • sudo apt-cache policy <package-name> - Shows which version is currently installed and which versions are available to install
  • sudo apt search <package-name> -  Show detailed information about the package.
  • sudo apt show <package-name> -  Show Detailed info with description and depends.
  • while! <command>; do sleep 1; done = Repeating a command until it successfully completes.
  • Artsy Commands:
    • figlet -f <font> "<text>" = allows us to draw 3D-style ASCII text art using a huge library of different font files. E.g:
      • figlet -f basic "Hello Word"
    • aafire -driver curses = fill your terminal window with a simulated ASCII open fire. 
    • cbonsai -m <message> = 
      • cbonsai -S = screensaver mode
    • convert file.png file.pgm && aview file.pgm = turn any standard image file into an ASCII equivalent that the terminal can open
    • cmatrix = similar to Matrix movies
      • cmatrix -s = run it full screen as a screensaver.
  • SystemD / SystemCtl:
    • Important note: The environment variables are not "visible" by the service unit. If your service unit needs to use environment variables you need to declare them into the service unit configuration file. (e.g: [Service] Environment="GITHUB_TOKEN=xpto...")
    • systemctl --type=service - List all loaded services on your system (whether active; running, exited or failed)
    • systemctl -l -t service | less - List all Systemd services
    • systemctl start <application-name>.service  - Start a systemd service.
    • systemctl stop <application-name>.service  - Stop a currently running service.
    • systemctl restart <application-name>.service  - Restart a currently running service.
    • systemctl reload <application-name>.service  - Reload its configuration files.
    • systemctl enable <application-name>.service   - To start a service at boot.
    • systemctl disable <application-name>.service   - To disable the service from starting automatically.
    • systemctl status <application-name>.service  - To check the status of a service.
    • systemctl list-units  - To see a list of all of the active units that systemd knows about.
    • systemctl list-dependencies <application-name>.service - To see a unit’s dependency tree.
    • systemctl show <application-name>.service - To see the low-level properties of a unit.
    • systemctl reset-failed - Resets any units from failed state
    • systemctl daemon-reload - Reload the service files to include a new service.
  • Service - The service command is used to manage running services
    • service <service-name> start / stop / restart / status
    • service <service-name> reload - Reload the configuration files for the service after they have been edited.
    • chkconfig --list - Displays the current service configuration
  • Number of computer Cores:
    • grep -c ^processor /proc/cpuinfo
    • nproc --all
    • getconf _NPROCESSORS_ONLN       # works on macOS
    • grep -c 'cpu[0-9]' /proc/stat
  • Ping:
    • sudo nmap -sn -PE 192.168.1.0/24 - Ping all 256 IPs on the 192.168.1.0 network. Using /24 will search 256 addresses, /23 will search 512 addresses, /22 will search 1024. Options meaning:  -sn (No port scan) -PE (ICMP ping echo).
    • ping -c 3 192.168.1.255 | grep 'bytes from' | awk '{ print $4 }' | sort | uniq   -   Ping the broadcast address to return all devices on the network.
    • ping 192.168.1 | arp -a   -   Ping all IP's and return their MAC address.
    • for ip in {1..254}; do ping -c 1 -W 1 192.168.1.$ip | grep "64 bytes"; echo -ne $(( 100 * $ip/254)) % done \\r; done - Ping all IP's in the range of the "for"
  • tail [-n 10] /var/log/ppp.log  -  View the VPN PPP log. (-n = Number of lines, default is 10)
  • DNS:
    • nmcli dev show 2>/dev/null | grep DNS | sed 's/^.*:\s*//'   -  Show DNS server IPs.
    • systemd-resolve --status | grep 'DNS Servers' -A2  -  Show DNS server IPs.
  • traceroute - Investigate the routes of network packets.
  • Check Memory Use:
    • free -m -t - used/available memory and the swap memory in megabytes.
    • cat /proc/meminfo - It contains real-time information about the system’s memory usage.
    • vmstat - provides general information about processes, memory, paging, block IO, traps, and CPU activity.
    • top - useful to check memory and CPU usage per process
    • htop - A complete overview of all processes running on your system, along with details such as process IDs, CPU and RAM usage, and how long they've been running.
    • System Monitor - Checking Memory Usage in Linux using the GUI
  • Command Grouping in Bash:
    • Placing commands in () creates a subshell in which the grouped commands are executed. That means that any changes to variables made in subshell, stay in subshell.
      • (cd /workspace/foo && python main.py) & (cd /workspace/boo && python main,py)
Autostart:
  • Autostart scripts no Ubuntu: Add the script to the /etc/init.d/rc.local file
  • Example:
  • - export SOMEPATH="/some/path:$SOMEPATH"
  • - cd /opt/test
  • - sh /opt/test/test.sh
  • - echo 'Test loaded!'
  • To remove autostart service:
  • sudo update-rc.d apache2 disable

Files:
  • /etc/init.d/rc.local => Can be used to autostart applications.
  • /etc/rc.local => Can be used to autostart applications.
  • ~/.bashrc => Can be used to autostart applications.
  • ~/.bash_history => History of commands executed by the user in the terminal (Ex: /root/.bash_history ?)
  • /etc/resolv.conf  =>  DNS configuration file
  • /etc/apt/sources.list  =>  apt-get configuration file
  • ls /etc/apt/sources.list.d  => apt-get repository names
  • /etc/network/interfaces  =>  Network interfaces configuration file
Others:
  • "Could not get lock /var/lib/dpkg/lock" or "unable to acquire the dpkg frontend lock"
    • sudo rm /var/lib/dpkg/lock
    • sudo rm /var/lib/apt/lists/lock
    • sudo rm /var/cache/apt/archives/lock
    • [Optional] sudo dpkg --configure -a
  • Create a new json file with "{}" content for each file found at folder ".":
    • find . -exec /bin/sh -c ‘echo \{\} > {}.json’ \;

References: