Skip to content
Symrak edited this page Feb 18, 2019 · 35 revisions

Linux systems useful commands list


Commands and Utilities

apt utility

Listing all installed packages in the system

Next command will print out a list of installed packages: sudo apt list --installed.

Source: askubuntu.com


dd command

Hard drive data backup and restore

To make an image of data, stored on hard drive, next command must be used:
sudo dd if=<Source hard drive> conv=sync,noerror bs=64K status=progress | gzip -c > <Path to image file>.
Where:

  • <Source hard drive> – path to the hard drive, that will be backuped (for examle, /dev/sda);
  • <Path to image file> – path to the result file, which will contain data (like /media/user/usb/backup.img.gz).

To restore data to selected hard drive from a backup file, use this command:
gunzip -c <Path to image file> | sudo dd of=<Target hard drive> status=progress.
Where:

  • <Path to image file> – path to the file, which contains an reserve copy of the data (like /media/user/usb/backup.img.gz);
  • <Target hard drive> – path to the hard drive, which will be overwritten with restored data (for examle, /dev/sda).

Source: linux.com


mkdir command

Create all folders in specified path with selected access mode

Next command will create all folders in specified path even ever they are not exist: mkdir -p <Path to directory>.
Access mode can be assigned to the newly created directories using -m key: mkdir -p <Permission bits> <Path to directory>.
Where:

  • <Path to directory> – full path to the newly created directory (like, I know... /tmp/super-secret-folder);
  • <Permission bits> – linux permission bits in numeric format (like 0755 or 644).

Source: linux.die.net


screen utility

New screen with human-readable name

For creating a new screen with human-readable name use: screen -S '<Screen name>' <Command> command.

Source: stackoverflow.com


su command

Executing command from name of another user

For executing command from name of another user use: su <Username> -c "<Command>" command.

Source: stackexchange.com


ffmpeg utility

Video streaming using ffmpeg

Running ffserver command: ffserver -f <Config file>.
Where:

  • <Config file> – ffserver configuration file (/etc/ffserver.conf by default).

Launching ffmpeg stream:
ffmpeg -f <Format selecting> -s <Resolution selecting> -framerate <FPS> -pixel_format <Pixel format> -i <Input source> <Output destination>.
Where:

  • <Format selecting> – format type (video4linux2 is suitable);
  • <Resolution selecting> – source resolution selecting (like 640x480 or another supported one);
  • <FPS> – source frames per second value that's needed (simple number like 30);
  • <Pixel format> – pixel format, that will be used (yuyv422 is good choice);
  • <Input source> – selecting input source (/dev/video0 by default for video streaming from single web-cam);
  • <Output destination> – selecting output destination (http://<Host name>:<Port>/<Feed name> for streaming, where:
    • <Host name> – host name or IP address of the streaming host (localhost is good choice!);
    • <Port> – selected port for the streaming server (from the /etc/ffserver.conf file, 8090 by default);
    • <Feed name> – name of the configured feed (from the /etc/ffserver.conf file, feed1.ffm by default).

Sources: acmesystems.it, github.com


smbclient utility

Copying files from local machine to remote SMB server

For copying files recursivelly from local machine to remote SMB server use next command:
smbclient //<Remote SMB server address> -U <Username> -c 'prompt OFF; recurse ON; cd <Remote directory>; lcd <Local directory>; mput *'.

Source: stackoverflow.com


watch command

Repeating command every X seconds

For repeating specified command every X seconds you can use next construction:
watch -n X <Command with arguments>.

Source: tecmint.com


System Information

Getting CPU information

This command will show-off all technical information about installed CPU: lscpu.

Source: linux.die.net


Showing all available partitions

To view all partitions just use: sudo fdisk -l.
Also, lsblk command can be used.

Source: linux.die.net


Checking HDD health state (smartmontools)

To show S.M.A.R.T. capabilities of the device: sudo smartctl -c <Disk name>.
To show S.M.A.R.T. status of the device: sudo smartctl -H <Disk name>.
To show all S.M.A.R.T. information of the device: sudo smartctl -a <Disk name>.

Where:

  • <Disk name> – disk name (like /dev/sda).

Source: linux.die.net


Networking

Checking for running services

To check all running services, that are listening on TCP and UDP ports, this command will be useful:
netstat -lntu. Flags, that can be used:

  • -l – show only services which are listening on some port;
  • -n – show port number (don't try to resolve the service name);
  • -t – include TCP ports;
  • -u – include UDP ports;
  • -p – include name of the program.

Source: superuser.com


User Management

List all users in the system

To list all of existing local users in the system this command will be useful:
cut -d: -f1 /etc/passwd.

Source: askubuntu.com


Add an existing user to an existing secondary group

To add an existing user to an existing secondary group must use this command:
usermod -a -G <Group name> <User name>.

Source: linux.die.net


Services and their configs

Service Packet name Config files Service file
SAMBA samba /etc/samba/smb.conf /etc/init.d/samba
TFTP tftpd-hpa /etc/default/tftpd-hpa /etc/init.d/tftpd-hpa
/etc/tftpd.remap
NFS nfs-kernel-server /etc/exports /etc/init.d/nfs-kernel-server
nfs-common

VMware Virtualization

VMware Tools installation

VMware Tools can be installed in two ways:

  • from CD disc, using vSphere or Workstation clients;
  • from repository, using command sudo apt-get install open-vm-tools.

Source: vmware.com, kb.vmware.com


Others

Merge several files into one

Next command can be used to merge several files into single one:
cat <Path to source files> > <Path to result file>
Where:

  • <Path to source files> – can contain file type mask (like *.txt);
  • <Path to result file> – output file name or path to it.

Source: stackexchange.com


Remove packet including its config files

To remove installed packet with configuration files, associated with it, next command will be helpful: sudo apt-get purge <Package name>.

Source: askubuntu.com


View total directory size or single file size

For viewing total directory size or single file size in human-readable format, just use command:
du -sh <Path to directory or file>

For filtering items by size with limitation by directory level can be used next command:
du -h --threshold=<Size> --max-depth=<Depth> <Path to directory or file>
Where:

  • <Size> – limit size (K, M, G and others postfixes are accepted);
  • <Depth> – max level of scanning (simple number, e.g. 1, 2, 3).

Source: stackexchange.com


File content output without comments

Next command will execute file content output without comments:
cat <Path to file> | egrep -v "^\s*(#|$)".

Source: kvz.io


Find files matching specified name

For searching for files with known name find <Search directory> -name <Needed file name> command can be used.

Source: codecoffee.com


Find files which containing specific string

For searching for files which containing specific string, just use command:
grep -rnw <Search directory> -e <Specific string>.

Source: stackoverflow.com


Change permissions only on files

For changing permissions only on files in selected directory, next command can be used:
find <Path to the directory> -type f | sudo xargs chmod <Permissions as octal value>.

Source: askubuntu.com


Applying action on service when interface event has happened

To assign an action on event, generated by selected networking interface, simple script, placed in /etc/network/<Event type> location can be used.
Where:

  • <Event type> – Directory name, associated with event type:
    • if-down.d – directory for scripts, applying when interface is going down;
    • if-post-down.d – directory for scripts, applying after interface going down event;
    • if-pre-up.d – directory for scripts, applying before interface going up event;
    • if-up.d – directory for scripts, applying when interface is going up.

Script example listed below:

#!/bin/sh
if [ "$IFACE" = <Interface name> ]; then
<Required action>
fi
exit 0

Source: askubuntu.com


Measuring command time execution

To measure execution time of command or program command time can be used:
time <command>.

Source: stackoverflow.com


Add Full-HD resolution on VM linux

To add the Full-HD resolution on linux machine, that's running in virtual environment, use next commands from the list bellow.
Before using them please refer to the manual on source link and validate proper names of devices and modes for your installation.
gtf 1920 1080 60
xrandr --newmode "1920x1080_60.00" 172.80 1920 2040 2248 2576 1080 1081 1084 1118 -HSync +Vsync
xrandr --addmode Virtual1 "1920x1080_60.00"
xrandr --output Virtual1 --mode "1920x1080_60.00"

Source: stackexchange.com


Clone this wiki locally