Linux Basics

File Operations

Archive Files

zip

# Create an achive file
zip -r achive.zip dir_name

# List files of the achive file
unzip -l achive.zip

# Extract files
unzip achive.zip
unzip achive.zip -d /target/filepath

tar

# Create an achive file
tar -czvf achive.tar.gz dir_name
tar -cjvf achive.tar.bz2 dir_name
tar -cvf achive.tar dir_name

# List files of the achive file
tar -tf achive.tar.gz

# Extract files
tar -xvf achive.tar.gz
tar -xvf achive.tar.gz -C /target/filepath

Get information of file

Real path

# Print real path of file
realpath path/to/file

Architecture of software

# Architecture of software
cat `which {your_software}` | file -

# file type
file path/to/file

Files and Directories

Directories

Directories Description
/bin binary or executable programs.
/boot It contains all the boot-related information files and folders such as conf, grub, etc.
/dev device files such as dev/sda1, dev/sda2, etc.
/lib kernel modules and a shared library.
/etc system configuration files.
/home home directory. It is the default current directory.
/media removal media devices are inserted
/mnt temporary mount point
/opt optional or third-party software.
/proc It is a virtual and pseudo-file system to contains info about the running processes with a specific process ID or PID.
/root root home directory
/run It stores volatile runtime data. run time variables.
/sbin binary executable programs for an administrator.
/sys It is a virtual file system for modern Linux distributions to store and allows modification of the devices connected to the system.
/tmp temporary space, typically cleared on reboot.
/usr User related programs.
/var variable data files. log files.
  • /lib: It is a place for the essential standard libraries. Think of libraries required for your system to run. If something in /bin or /sbin needs a library that library is likely in /lib.
  • /usr
    • /usr/bin: Executables binary files. E.g. java, mvn, git, apt, kill.
    • /usr/local: To keep self-compiled or third-party programs.
    • /usr/sbin: This directory contains programs for administering a system, meant to be run by ‘root’. Like ‘/sbin’, it’s not part of a user’s $PATH. Examples of included binaries here are chroot, useradd, in.tftpd and pppconfig.
    • /usr/share: This directory contains ‘shareable’, architecture-independent files (docs, icons, fonts etc).
    • /usr/lib: the /usr directory in general is as it sounds, a user based directory. Here you will find things used by the users on the system. So if you install an application that needs libraries they might go to /usr/lib. If a binary in /usr/bin or /usr/sbin needs a library it will likely be in /usr/lib.
  • /var: variable data files. log files.
    • /var/lib: the /var directory is the writable counterpart to the /usr directory which is often required to be read-only. So /var/lib would have a similar purpose as /usr/lib but with the ability to write to them.

Configuration Files

Bash Configuration Files

File Description
/etc/profile This is a “System wide” initialization file that is executed during login. This file provides initial environment variables and initial “PATH” locations.
/etc/bashrc This again is a “System Wide” initialization file. This file is executed each time a Bash shell is opened by a user. Here you can define your default prompt and add alias information. Values in this file can be overridden by their local ~/.bashrc entry.
~/.bash_profile If this file exists, it is executed automatically after /etc/profile during the login process. This file can be used by each user to add individual entries. The file however is only executed once at login and normally then runs the users .bashrc file.
~/.bash_login If the “.bash_profile” does not exist, then this file will be executed automatically at login.
~/.profile If the “.bash_profile” or “.bash_login” do not exist, then this file is executed automatically at login.
~/.bashrc This file contains individual specific configurations. This file is read at login and also each time a new Bash shell is started. Ideally, this is where you should place any aliases.
~/.bash_logout This file is executed automatically during logout.
~/.inputrc This file is used to customize key bindings/key strokes.

Most global config files are located in the /etc directory

File Description
/etc/X11/ xorg specific config files
/etc/cups/ sub-directory containing configuration for the Common UNIX Printing System
/etc/xdg/ global configs for applications following freedesktop.org specification
/etc/ssh/ used to configure OpenSSH server behavior for the whole system
/etc/apparmor.d/ contains config files for the AppArmor system
/etc/udev/ udev related configuration

Important Global Config Files

File Description
/etc/resolv.conf used to define the DNS server(s) to use
/etc/bash.bashrc used to define the commands to execute when a user launches the bash shell
/etc/profile the login shell executes the commands in .profile script during startup
/etc/dhcp/dhclient.conf stores network related info required by DHCP clients
/etc/fstab decides where to mount all the partitions available to the system
/etc/hostname set the hostname for the machine
/etc/hosts a file which maps IP addresses to their hostnames
/etc/hosts.deny the remote hosts listed here are denied access to the machine
/etc/mime.types lists MIME-TYPES and filename extensions associated with them
/etc/motd configure the text shown when a user logs in to the host
/etc/timezone set the local timezone
/etc/sudoers the sudoers file controls the sudo related permission for users
/etc/httpd/conf and /etc/httpd.conf.d configuration for the apache web server
/etc/default/grub contains configuration used by the update-grub for generating /boot/grub/grub.cfg
/boot/grub/grub.cfg the update-grub command auto-generates this file using the settings defined in /etc/default/grub

Important User-Specific Config Files

File Description
$HOME/.xinitrc this allows us to set the directives for starting a window manager when using the startx command
$HOME/.vimrc vim configuration
$HOME/.bashrc script executed by bash when the user starts a non-login shell
$XDG_CONFIG_HOME/nvim/init.vim neovim configuration
$HOME/.editor sets the default editor for the user
$HOME/.gitconfig sets the default name and e-mail address to use for git commits
$HOME/.profile the login shell executes the commands in the .profile script during startup
$HOME/.ssh/config ssh configuration for a specific user

System Settings

System Time

Time

# show date time
date

# date time format
date '+%Y-%m-%d'
date '+%Y-%m-%d %H:%M:%S'
date '+%Y-%m-%d_%H-%M-%S'

# update time and date from the internet
timedatectl set-ntp true

Timezone

# list timezones
timedatectl list-timezones

# set timezone
timedatectl set-timezone Asia/Shanghai

# show time settings
timedatectl status

hostname

The hostname is used to distinguish devices within a local network. It’s the machine’s human-friendly name. In addition, computers can be found by others through the hostname, which enables data exchange within a network, for example. Hostnames are used on the internet as part of the fully qualified domain name.

you can configure a computer’s hostname:

# setting
$ hostnamectl set-hostname server1.example.com
# verify the setting
$ less /etc/hostname
# query your computer's hostname
$ hostname

hosts

The /etc/hosts file contains the Internet Protocol (IP) host names and addresses for the local host and other hosts in the Internet network. This file is used to resolve a name into an address (that is, to translate a host name into its Internet address).

sudo vim /etc/hosts

Add Environment Variables

/etc/profile

Add environment variables

cp /etc/profile "/etc/profile.bak.$(date '+%Y-%m-%d_%H-%M-%S')"
echo "export name=value" >> /etc/profile
cat /etc/profile
source /etc/profile

Add to path

cp /etc/profile /etc/profile.bak.$(date '+%Y-%m-%d_%H-%M-%S')
echo 'export PATH=$PATH:/usr/local/mysql/bin' >> /etc/profile
source /etc/profile

System Log

# print OOM killer log
dmesg -T | egrep -i 'killed process'

Upload File

scp

SCP will always overwrite existing files. Thus, in the case of a clean upload SCP should be slightly faster as it doesn’t have to wait for the server on the target system to compare files.

# transfer a file
scp local_file remoteuser@remote_ip_address:/remote_dir
# transfer multiple files
scp local_file1 local_file2 remoteuser@remote_ip_address:/remote_dir

# transfer a directory
scp -r local_dir remoteuser@remote_ip_address:/remote_dir
# transfer a file from remote host to local
scp remoteuser@remote_ip_address:/remote_file local_dir
# Transfer Files Between Two Remote Systems 
scp remoteuser@remote_ip_address:/remote_file remoteuser@remote_ip_address:/remote_file
  • -P SSH_port

rsync over ssh

In the case of a synchronization of files that change, like log files or list of source files in a repository, rsync is faster.

Copy a File from a Local Server to a Remote Server with SSH

rsync -avzhe ssh backup.tar.gz root@192.168.0.141:/backups/
# Show Progress While Transferring Data with Rsync
rsync -avzhe ssh --progress backup.tar.gz root@192.168.0.141:/backups/

Copy a File from a Remote Server to a Local Server with SSH

rsync -avzhe ssh root@192.168.0.141:/root/backup.tar.gz /tmp

sftp

sftp [username]@[remote hostname or IP address]

# download file to the local system's Home directory.
get [path to file]
# change directory
get [path to file] [path to directory]
# change filename
get [path to file] [new file name]

# upload the local system's Home directory's file to the remote server's current directory
put [path to file]
# change directory
put [path to file] [path to directory]
# change filename
put [path to file] [new file name]

Application Data

logging file path: /var/log/{application_name}

upload file path: /data/{application_name}/upload

application build and running file path: /var/java/{application_name}, /var/html/{application_name}

References