List of useful commands

A repository of useful commands for POSIX systems such as Unix and Linux.

Rosetta stone for Unix/Linux

Before, we go into more details about specific Linux or Unix commands, there is a really nice web site that tries to gather most common used commands on various Unix and Linux flavour in a table. It is then easy to find equivalences of commands between all those variants. The web site is called the Rosetta Stone for Unix.

General

File commands

Diff & patch

This set of two commands allow to make a difference between two version of a complete file tree and apply it to another tree. This can be really useful let say you are downloading a Blog or Wiki software (version X) and installing it on your personal server. There are a few minor things that annoy you but seems to be OK for a vast majority of person. Therefore, you modify (tune) a few files of the software and use it as is (version Xm). A security update of the software is now available (version Y). If you update to version Y, you loose your change of version Xm. A good solution is to make a difference between version X and Y and apply those same changes to version Xm which become Ym then . The commands are:

RPM based package system

Then on Fedora (up to release 21) and CentOS/RHEL, there is yum which allows to manage online repository of rpm packages.

yum upgrade:

yum install packages: install package(s) from the repositories

yum remove packages: remove an installed package(s)

yum list: List packages (installed and available)

Not always present/install are plugins for yum to check which services need to be restarted after an upgrade.

yum ps based on the yum ps plugin;

needs-restarting based on ;

Without using yum, and it works for all Linux systems (whether using RPM or not)

lsof +L1 -R

Check in the list of files (rightmost column) that the files are under a system folder such as /usr

Fedora 22 and newer have replaced yum by dnf and it is possible that future releases of CentOS/RHEL will follow suit.

dnf upgrade:

dnf install packages: install package(s) from the repositories

dnf remove packages: remove an installed package(s)

dnf list: List packages (installed and available)

Services management

Red Hat-like systems (Red Hat, Fedora, Mandriva, Suse, etc.) offer the 'chkconfig' command to manage services/runlevel. You can configure at which runlevels the service is active or not. For example, activating ssh server only in runlevel 3 and 5 will be done like that:

$ sudo chkconfig --level 35 sshd on

The activation or not of a service will be taken into account at next restart unless you manually start and stop them using the /etc/init.d scripts or the service command.

$ sudo service sshd start

Here are the common parameters for the service (or init.d scripts): start, stop, restart, reload, status

Debian-like systems (incl. Ubuntu) do not provide the chkconfig command. They provide the 'update-rc.d' command that mimic the functionnality of the chkconfig one. They provide also another command, 'invoke-rc.d', that allows to restart, stop and start services.

I am not going to compare technically the pro's and con's of upstart vs systemd here, but clearly for the user space tools to manipulate both systems, systemd has one entry point and that makes thing simpler IMHO.

Mass storage management

UUID - device unique identifier

In recent Linux distribution, the configuration of device does not rely only on device names. One can now make it rely on “label” or “uuid”. A “uuid” is a unique identifier for a device, it can be practical is the device name change often, because its uuid will always be the same.

To find out the UUID of a device, use the following command:

$ sudo vol_id -u <device>

One can replace “<device>” by a partition name like /dev/hda1 or /dev/sda5, etc.

File permission

The set of commands below will create a directory readable by root and users belonging to a group, everyone else can't read it. Only root can write into it.

The two first line are obvious. The third line set the permission as rwx for the user, rx for the group and none for others. In addition the setgid bit (g+s or the '2' in '2750') is used to preserve the group ownership of the parent directory to newly create file under it. The last command (only works if your underlying file system supports ACL) set the default permission of rwx for users, rx for group and none for others.

With the above setup creating a file under this directory will result in preserving the ownership and rights of the parent directory:

Tunnelling

Your goal is to access a service on a dedicated host with everything going through SSH. Why? On a remote machine, the only service accessible from the outside World is the SSH service, but you still want to access other services which are behind the firewall of this remote machine.

It will request you a password. After this the SMB share folder is mapped to the directory /mnt/share. If you go exploring with Nautilus, you go to “File System” → “mnt” → “share” and then you can access and open all of your files.

To make it automatic, you could add a line in the file that manage partitions and disks (/etc/fstab).

A bit of explanation, the spaces in the share drives have to be replace by '\040', and the login and password are going to be place in a file that is private to the super user (not readable by others). Sample content for the credentials file:

Development

Version Control System

Git VCS

The following list is just a reminder for most used commands. Please see the main article about git for more comprehension.

'git status' to get the states of which files are staged (will be 'commit'), which files have been modified but not yet staged and which files aren't tracked

'git add <file|directory>' to add a list of files or directories to the staged status

'git rm <file|directory>' to remove a list of files or directories from the current local repository, use the option –cached before the list of files/dir if you do not want the files to be removed from the working directory

'git commit' will commit the list of files and directories currently staged

'git pull origin'

'git push origin'

'git checkout HEAD – <file>' just in case your deleted <file>, you can restore it with this command