Skip to content

Instantly share code, notes, and snippets.

@raphaelchaib
Last active May 30, 2020 15:41
Show Gist options
  • Save raphaelchaib/1fdc97ad4212610139655cf12ae61ac8 to your computer and use it in GitHub Desktop.
Save raphaelchaib/1fdc97ad4212610139655cf12ae61ac8 to your computer and use it in GitHub Desktop.
Linux Essential Commands

PATH

  • System wide PATH configuration

Edit /etc/environment with:

PATH='/foo/bar:'$PATH

Reload it with: source /etc/environment

  • Temporary PATH's new config on shell
export PATH='/foo/bar:'$PATH

Shell

  • Install ZSH
sudo apt-get install zsh

oh my zsh How to install

  • oh my zsh Basic install via cURL:
sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"

... or via wget:

sh -c "$(wget https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh -O -)"
  • Change default shell to "ZSH"

The new shell needs to be authorized at /etc/shells.

Note that this will not work if Zsh is not in your authorized shells list (/etc/shells) or if you don't have permission to use chsh. If that's the case you'll need to use a different procedure.

chsh -s $(which zsh)
  • Reload shell
us@ubuntu:~/ > source ~/.zshrc

or

us@ubuntu:~/ > . ~/.zshrc
  • Verify default shell
echo $SHELL

Root

  • Open interactive terminal
sudo -s
  • Use terminal as superuser
sudo su
  • Make things disappear and restart without any reason at all (that's a good thing... Really!)
sudo rm -rvf $HOME

Users commands

General

  • Kill logged in user
sudo pkill -KILL -u <user>
  • Use terminal as another user
sudo su <user>

Permissions

  • Change folders/files owner, recursively
sudo chown user:group -R folder/file # -R > recursive

NoPassword for sudo (sudoers)

<username> ALL=(ALL) NOPASSWD:ALL

Files and Folders

  • Search for string in files and show filename and line numbers (ref)
sudo grep -rn '.' -e "SEARCHSTRING"
  • Search and replace strings in files (ref)
sudo grep -rl '.' -e "SEARCHSTRING" | xargs sed -i 's/SEARCHSTRING/FINALSTRING/g'
  • Remove lines from files

To remove the last line from a file without reading the whole file or rewriting anything, you can use

tail -n 1 "$file" | wc -c | xargs -I {} truncate "$file" -s -{}

To remove the last line and also print it on stdout ("pop" it), you can combine that command with tee:

tail -n 1 "$file" | tee >(wc -c | xargs -I {} truncate "$file" -s -{})

Media Devices

  • List disks UUIDs
blkid
  • List device disks
fdisk -l
  • Create bootable ISO
# Manjaro
dd bs=4M if=/path/to/manjaro.iso of=/dev/sd[drive letter]

SSH

  • Removing modified host identification key from known_hosts
ssh-keygen -f "/home/<user>/.ssh/known_hosts" -R <host>

FTP

Terminal

The following examples illustrate typical uses of the command ftp for remotely copying, renaming, and deleting files.

This command will attempt to connect to the ftp server at abc.xyz.edu. If it succeeds, it will ask you to log in using a username and password. Public ftp servers often allow you to log in using the username "anonymous" and your email address as password. Once you are logged in you can get a list of the available ftp commands using the help function:

ftp abc.xyz.edu 

This lists the commands that you can use to show the directory contents, transfer files, and delete files.

ftp> help

This command prints the names of the files and subdirectories in the current directory on the remote computer.

ftp> ls

This command changes the current directory to the subdirecotry "customers", if it exists.

ftp> cd customers

Changes the current directory to the parent direcotry.

ftp> cd ..

Changes the current directory [em]on the local computer[/em] to "images", if it exists.

ftp> lcd images

Changes to "ascii" mode for transferring text files.

ftp> ascii

Changes to "binary" mode for transferring all files that are not text files.

ftp> binary

Downloads the file image1.jpg from the remote computer to the local computer. Warning: If there already is file with the same name it will be overwritten.

ftp> get image1.jpg

Uploads the file image2.jpg from the local computer to the remote computer. Warning: If there already is file with the same name it will be overwritten.

ftp> put image2.jpg

A '!' in front will execute the specified command on the local computer. So '!ls' lists the file names and directory names of the current directory on the local computer.

ftp> !ls

With mget you can download multiple images. This command downloads all files that end with ".jgp".

ftp> mget *.jpg

Uploads all files that end with ".jgp".

ftp> mput *.jpg

Deletes all files that end with ".jgp".

ftp> mdelete *.jpg

Turns iteractive mode on or off so that commands on multiple files are executed without user confirmation.

ftp> prompt

Exits the ftp program.

ftp> quit

Go Lang

Download the lib and put it into /usr/local/go.

  • PATH and GOPATH

Add following lines to ~/.profile, for your own user, or /etc/profile, for system-wide.

Remember to create the directory for your GOPATH.

export GOPATH=$HOME/Dev/_go
export PATH=$PATH:/usr/local/go/bin:$GOPATH/bin

CentOS

  • Configure nameservers in /etc/resolv.conf
nameserver 8.8.8.8
nameserver 8.8.4.4

Fixing errors

  • Reinstall app indicators at Ubuntu Systray
sudo apt-get remove libappindicator1 # if it is installed
sudo apt-get install libappindicator1

Logs

  • Common Linux log files names and usage

/var/log/messages : General message and system related stuff

/var/log/auth.log : Authenication logs

/var/log/kern.log : Kernel logs

/var/log/cron.log : Crond logs (cron job)

/var/log/maillog : Mail server logs

/var/log/qmail/ : Qmail log directory (more files inside this directory)

/var/log/httpd/ : Apache access and error logs directory

/var/log/lighttpd/ : Lighttpd access and error logs directory

/var/log/boot.log : System boot log

/var/log/mysqld.log : MySQL database server log file

/var/log/secure or /var/log/auth.log : Authentication log

/var/log/utmp or /var/log/wtmp : Login records file

/var/log/yum.log : Yum command log file.

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