Akiq2016/blog

Linux command notes and cheatsheet

Opened this issue · 0 comments

Shortcuts

CTRL L # Clear the terminal
CTRL A # Cursor to start of line
CTRL E # Cursor the end of line
CTRL W # Delete word on the left
CTRL U # Delete left of the cursor
CTRL K # Delete right of the cursor
CTRL R # reverse search history
!! # repeat last command

Basic file manipulation

cp image.jpg newimage.jpg # copy and rename a file
cp image.jpg <folderName>/ # copy to folder
cp image.jpg folder/sameImageNewName.jpg
cp -R stuff otherStuff # copy and rename a folder
cp *.txt stuff/ # copy all of *<file type> to folder

mv file.txt Documents/ # move file to a folder
mv <folderName> <folderName2> # move folder in folder
mv filename.txt filename2.txt # rename file
mv <fileName> stuff/newfileName
mv <folderName>/ .. # move folder up in hierarchy

rm -i <fileName> .. # ask for confirmation each file
rm -f <fileName> # force deletion of a file
rm -r <foldername>/ # delete folder
rm <fileName> .. # delete file (s)

ln file1 file2 # physical link
ln -s file1 file2 # symbolic link

Show the manual for command

man xxx

Set the system's host name: root@xxx

# update hostname
echo "cutecute" > /etc/hostname

# Read the host name from the specified file.
hostname -F /etc/hostname

# next time you log in, you will see the new host name

Check IP address in linux

# ifconfig stands for "interface configuration".
# can view and change the configuration of the network interfaces on your system.

# display information about all network interfaces currently in operation.
ifconfig

# To view the configuration of a specific interface, specify its name as an option.
ifconfig eth0

Redirect an IP Address Using a Hosts File

# The hosts file is used to map hostnames (in other words domains) to IP addresses.
# With the hosts file,
# you can change the IP address that you resolve a given domain name to.
# This change only affects your own computer,
# without affecting how the domain is resolved worldwide.

# Before the advent of DNS,
# the host table was the only way of resolving hostnames on the fledgling Internet.

# check manual pages
man hosts

# tips: cannot map IP to IP.
# IP_address canonical_hostname [aliases...]

Choose time zone

# print current date
date

# select time zone
dpkg-reconfigure tzdata

Install package on Linux(Debian, Ubuntu)

Installing Software on Linux
What is the difference between dpkg and aptitude/apt-get
apt-vs-apt-get-difference

# you will need to become SuperUser to install software.
apt install ${packagename}
apt remove ${packagename}
# if you want to remove all its files including configuration files
apt purge ${packagename}

# Although the repositories that contain installable packages might live on the Internet
# or on a disc somewhere,
# APT keeps a local database on your hard drive with a list of all available packages
# and where to find them.
# This database needs to be explicitly updated.
apt update
# A common idiom is to update your package database,
# and then upgrade all the packages that have patches or security updates to install.
apt upgrade

Add/Modify user

linux-users-and-groups

# Leveraging Users and GroupsPermalink

# In many cases, user permissions are used to provide your system with greater security
# without any direct interaction.
# Many operating systems create specific system user accounts for different packages
# during the installation process.

# The best practice is to give each user their own login to your system. 
# This protects each user’s files from all other users.
# Furthermore, using specific accounts for users allows more accurate system logging,
# particularly when combined with tools like sudo.
# We recommend avoiding situations where more than one individual knows the password for a user account for maximum security.

whoami # root

apt-get install adduser
adduser aki
adduser aki sudo

sudo passwd <AccountName> # change a user's password

Change file/directory mode

# chmod <octal or letters> <file/directory name>

# we'd better use octal format to change mode, it's more quickly to set
chmod 111 test.sh

# show all files/diretories long list
ls -al

drwxrwxr-x 2 root root 4096 Feb  9 21:07 .
drwxr-xr-x 5 root root 4096 Feb  9 22:10 ..
---x--x--x 1 root root    5 Feb  9 21:07 test.sh

# each colum indicates:
# access permissions: drwxrwxr-x
# numbers of files or dirs: 2
# owner: root
# group: root
# size: 4096
# last access's time/date: Feb  9 21:07
# name: .

# The first ten characters show the access permissions.
# first characters indicates the type of file.
# next three define the owner’s permission to the file.
# next three define the permissions for the members of the same group as the file owner.
# The last three characters define all other users'.

# r: read 4
# w: write 2
# x: execute 1

Variable related

#!/bin/sh
echo Hello World

# The first line tells Unix that the file is to be executed by /bin/sh.
# This is the standard location of the Bourne shell on just about every Unix system.
# If you're using GNU/Linux,
# /bin/sh is normally a symbolic link to bash or more recently, dash.

# The only exception is when the very first line of the file starts with #! - as ours does.
# This is a special directive which Unix treats specially.

# use $ to refer a variable
echo $PATH

# export makes the variable available to sub-processes.
export var1="hello    world"

Searching / filter / replace: grep sed awk

grep-vs-awk-examples-for-pattern-search
what-are-the-differences-among-grep-awk-sed

# we can use grep to grab stuffs, while sed can grab and modify and do other things, awk do much more things as it is an entire programming language. For the basic usage, they all can do searching.

# search for pattern in files
grep [pattern] [files]

# search recursively for pattern in dir
grep -r [pattern] [dir]

# search for patterns in the output of command
[command output] | grep [pattern] 

# perform basic text transformations on an input stream
# s/regexp/replacement/
# -e script, --expression=script .   add the script to the commands to be executed
sed -e 's/day/night/g' -e 's/you/me/g' file 

# -i[SUFFIX], --in-place[=SUFFIX] .  edit files in place (makes backup if SUFFIX supplied)
sed -i.tmp s/dog/cat/g file

# scan per line and output the column $0 that includes 'aki' in aki.txt
awk '/aki/ { print $1 }' aki.txt

Process Management

linux-process-management

# show static process list
# By default ps selects all processes with the same effective user ID (euid=EUID)
# as the current user and associated with the same terminal as the invoker.
ps

# To see every process on the system using BSD syntax:
ps ax
ps axu

# show dynamic process list
top

# kill a process 
kill <PID>

# To start a process in the background (non-interactive), add a '&' at the end of a command
cp bigMovieFile.mp4 &

# know what is running in the background
jobs

# put a background process to foreground
fg %1 # (process 1)

Archive and compress data

how-to-archive-files-and-directories-in-linux-part-1

# c : Create an archive from a file(s) or directory(s).
# x : Extract an archive.
# f : Takes an argument that sets the name of the archive to operate upon.
# z : Compresses the archive using gzip compression method.
# v : View the progress while creating the archive.
# r : Append files to the end of an archive.
# t : List the contents of the archive.
# -C : To extract the archive in a different folder.

# create a new tar archive of the the directory akiFolder.
tar cf akitar.tar akiFolder/
# create a new gzipped archive of the the directory akiFolder.
tar czf akitar.tar.gz akiFolder/
# extracts the given archive file in anotherFolder directory
tar cf akitar.tar akiFolder/ -C anotherFolder/

# extract an archive in the current directory
tar xf akitar.tar
# extract the gzipped archive in the current directory
tar xzf akitar.tar.gz

# Exclude directories and/or files from while creating an archive
tar czvf akitar.tgz /home/aki --exclude=/home/aki/Downloads --exclude=/home/aki/Documents

# or using zip unzip package

SSH

SSH COMMAND
SSH PORT

# SSH, or Secure Shell, is a protocol used to securely log onto remote systems.
# It is the most common way to access remote Linux and Unix-like servers.

# `remote_host` is the IP address or domain name
# This command assumes that your username on the remote system
# is the same as your username on your local system.
ssh remote_host
# or you can specify it by using this syntax
ssh remote_username@remote_host
# connect to host on port. connect to port 22 by default
ssh -p port remote_username@remote_host

# configure SSH
# In Ubuntu, the main sshd configuration file is located at /etc/ssh/sshd_config.

# exit back into your local session
exit

# Create SSH Keys
ssh-keygen -t rsa

# Log Into SSH with Keys
# add your key to host for user to enable a keyed or passwordless login
# then next time you logging into the machine will not need to enter password
ssh-copy-id remote_username@remote_host

Network

ping hostname # ping host and output results
whois domain # get whois information for domain
dig domain # get DNS information for domain

# secure copy a file from remote server to the dir directory on your machine
scp user@host:file dir
# secure copy a file from your machine to the dir directory on a remote server
scp file user@host:dir

lsof -i tcp:1337 # list all processes running on port 1337

Machine Info

# disk: List information about all available or the specified block devices.
lsblk

# ram: Display amount of free and used memory in the system
free -g
free -m
free -k

# cpu: Display cpu info
cat /proc/cpuinfo