Linux: 200 commands for everyday life

Written on September 20, 2020

Category: Linux & Commands

Author: David Rodríguez, @davidjguru

Picture from Unsplash, by @lazycreekimages
Picture from Unsplash, user Michael Dziedzic, @lazycreekimages

For a long time I had the idea of gathering somewhere centrally the most common Linux commands in these years of technology work. Looking back, I see that my relationship with the software in a professional way (I was previously a trainee) began in 2007, about thirteen years ago. Since then, I have always been within Linux environments and above all, in Ubuntu / Debian in particular. In some moments I have had to manage CentOS / RHEL and in some laptops I still have Fedora. But Debian / Ubuntu / L-Ubuntu were always my options (and still are).

So I have compiled several files exported from the history command, some interesting utilities and I have tried to gather them all in this post, mainly those that can be a useful, fast and simple solution in the day to day.

I have thought of this grouping as a collection to get to know your system better and to be able to work better on a daily basis, losing the fear of the “Linux console”. With these instructions and the flexibility they provide (the commands have many more options than I summarize here) you can perform your daily work with comfort and reliability.

I thought of this little compilation as a list to quickly locate an option, understand how it works and apply it directly by copy/paste for testing. That’s why I have taken directly specific cases already parameterized with options and values. I think that somehow - and although it’s a bit simplistic - with these commands you can survive quite well on a day-to-day basis. You can get to know them better by running the command ‘man’ ìn prompt -> man grep, which gives you more information about each command line tool. I preferred to start from the general use or maybe more transversal and finish with the more specific, more related to the world of Drupal (Drush and Drupal Console commands). For my part, I only wish that I can be useful to you. In rereading the post I have seen the painting of a trajectory of more than ten years in the industry. It has been very beautiful. :_-)

Update (25/09/2020): Alfonso De Cala reviewed this article and left me a feedback through Linkedin. He noted that the post had repeated some commands and a couple of modifications, deprecated uses of commands: ifconfig and apt-get, for example. I have just made those changes and I leave here my note for your review. Thanks a lot for giving me feedback.


Table of Contents
1- Know your system (42 examples)

2- Using files and folders (55 examples)

3- Groups, users and permissions (15 examples)

4- Managing packages (26 examples)

5- Working in projects (57 examples)

6- :wq!


1- Know your system (42 examples)

I think that the important thing at the beginning is to be able to know better how to interact with the operating system, practicing with commands, so I have put this list of instructions in first place to get information. Note: Commands to getting information about your own Linux system.

1.1- Getting information from the system (19 examples)

  1. :~$ cat /etc/*release # How to know what Linux distro is.
  2. :~$ arch #​ Will show your system hardware architecture.
  3. :~$ uname -r # Will show the kernel version in use.
  4. :~$ dmidecode -q # Will show all the hardware elements present in your system.
  5. :~$ hdparm -i /dev/hda #​ Will show the features of the selected hard disk.
  6. :~$ cat /proc/cpuinfo # ​Will show information about your CPU.
  7. :~$ cat /proc/meminfo # Will verify the RAM memory comsuption in your system.
  8. :~$ free -h # Will show the current status and the use of Memory in your sistem using a human-friendly format.
  9. :~$ cat /proc/net/dev #​ Will show the network adapters and its statistics.
  10. :~$ cat /proc/mounts #​ Will show the mounted filesystem in your system.
  11. :~$ lspci -tv #​ Will show all the present PCI devices in your system.
  12. :~$ lsusb -tv # ​ Will show all the connected USB devices to your system.
  13. :~$ top #​ Will show the Linux Task using the max value of CPU.
  14. :~$ date # Will show the current date in your system.
  15. :~$ cal 2020 #​ Will show the whole calendar for the introduced year.
  16. :~$ cal 03 2020 #​ Like the previous, but only will show the month of the selected year.
  17. :~$ man ping # Will show pages from the help manual for a command, ping in this example.
  18. :~$ vmstat -s # Get a report about the memory comsuption in your system.
  19. :~$ fdisk -l # Returns a view with basic details with all available partition in your system.

1.2- Shutting down the system (7 examples)

  1. :~$ reboot # Just another command to restart your system by prompt.
  2. :~$ last reboot #​ Show a list with the last restarts.
  3. :~$ logout # It will close your current sesion.
  4. :~$ halt # Just other way for shutdown your system by prompt.
  5. :~$ shutdown -h 18:55 & #​ It will planificate the shutdown in the marked time.
  6. :~$ shutdown -c # Stops a planified shutdown of the system.
  7. :~$ shutdown -r now # It will restart your system just now.

1.3- Managing networks (16 examples)

  1. :~$ nmcli dev status # Checks all your network devices.
  2. :~$ ifup eth0​ # Will enable an eth0 interface.
  3. :~$ ifdown eth0​ # Will disable an eth0 interface.
  4. :~$ nmcli networking connectivity check # Checks the network connectivity state.
  5. :~$ dhclient eth0​ # Will enable an eth0 interface with dhcp mode.
  6. :~$ route -n​ # Will show a route table.
  7. :~$ route add -net 0/0 gw I ​ GATE_NAME​ # Will config an access gate.
  8. :~$ route del 0/0 gw ​ IP_ADRESS # Will disable an static IP.
  9. :~$ hostname​ # Will show the system hostname.
  10. :~$ ip link show​ # Will show the current status of the interfaces.
  11. :~$ mii-tool eth0​ # Will show the link status of ‘eth0’.
  12. :~$ ethtool eth0​ # Will show statistics from the eth0 network card.
  13. :~$ netstat -tup​ # Will show all active network connections.
  14. :~$ netstat -tupl​ # Will show all the listening services in networking and their PIDs.
  15. :~$ iwlist scan​ # Will show all the wireless networks.
  16. :~$ iwconfig eth1​ # Will show the configuration of a wireless network card.

2- Using files and folders (55 examples)

Then I think that on a daily basis, perhaps managing files (in Linux almost everything is a file) and directories is the next most important part. We are always moving from one folder to another and we are handling files, so it is important to familiarize yourself with these instructions. Note: Basic commands when you’re working with files and folders.

2.1- Basic usage (16 examples)

  1. :~$ mkdir name_folder # Will create a new folder with name.
  2. :~$ mkdir folder1 folder2 # Will create two folders in a same instruction.
  3. :~$ du -ma | sort -nr | head -n 20 # List 20 largest files and folders in current directory.
  4. :~$ rmdir my_folder # It will delete the my_folder directory, only if empty.
  5. :~$ rm -rf my_folder # It will delete the my_folder directory and all its content.
  6. :~$ mv old_folder new_folder # It will move or rename a folder with another.
  7. :~$ cp file_1 # Copy the file_1.
  8. :~$ cp file_1 file_2 # It will copy file_1 file_2 will copy the files simultaneously.
  9. :~$ cp -a folder1 # It will copy a whole folder.
  10. :~$ cp -a folder1 folder2 # It will copy the folders simultaneously.
  11. :~$ ln -s file link_tofile # It will create a symbolic link origin/destiny.
  12. :~$ ln file link_tofile # It will create a fisical link origin/destiny.
  13. :~$ file dump_file_database.sql # Returns the (true) type of file.
  14. :~$ rsync -rtv /custom/hello/ /drupal/hello/ # Copy recursive with timestamps and verbose
  15. :~$ rsync -avzh –progress –stats /home/drupal/ABCD/ /home/drupal/EFGH/ # Copy recursive with links and symbolic links, compressing, verbose, with human info and reporting progress.
  16. :~$ find /var/backups/backups_drupal/ -type f -name ‘*.tar.gz’ -mtime +10 -exec rm -f {} \; # Delete files older than 10 days

2.2- Moving between directories (12 examples)

  1. :~$ cd /home/user​ # Moves to the destiny folder.
  2. :~$ cd .. # Moves back to the previous level.
  3. :~$ cd ../.. # Moves back two levels.
  4. :~$ cd​ # Moves to the main folder of this unity.
  5. :~$ cd ~user # Goes to the personal folder of the marked user.
  6. :~$ cd – # Goes just to the former directory you was .
  7. :~$ pwd # Return the name of the current folder.
  8. :~$ ls # Show all resources in the current folder.
  9. :~$ ls -l # List all resources in the current folder with user, groups and permissions.
  10. :~$ ls -a # Show all resources in the current folder with the hidden files.
  11. :~$ ls -la // ll # Get a complete list of resources including hidden with all the data.
  12. ~$ tree # Show all content in a folder in a recursive way. Requires sudo apt install tree .

2.3- Looking for resources (9 examples)

  1. :~$ grep -i “string_to_search” filename # Search a specific string in a single file in case insensitive.
  2. :~$ grep “beginning*end” filename # Search string with RegExp over a single file.
  3. :~$ grep -iw “word” filename # Now we’re looking for a word in case insensitive.
  4. :~$ find / -name filename # Will search the file or folder starting by the root of system.
  5. :~$ find / -user username # Will look for files and folders owned by the username​.
  6. :~$ find /home/user -name *.bin # Will search all files with .bin extension in the folder.
  7. :~$ find /usr/bin -type f -mtime -5 #: Will search files created or changed from last five days.​
  8. :~$ whereis firefox # Will show the location of the binary file of firefox.
  9. :~$ which firefox # Will show the whole path to the marked binary / executable file.

2.4- Making copies and compressed files (18 examples)

  1. :~$ rsync -rtv /modules/custom/hello_world /workspace/drupal/hello_world # Sync folders.
  2. :~$ dump -0aj -f /tmp/home0.bak ​ /home​ #Creates a backup copy of the /home directory.
  3. :~$ dump -1aj -f /tmp/home0.bak ​ /home​ # Same as previous but generating incremental copy.
  4. :~$ find ​ /home/user​ -name ‘*.​ txt​ ’ | xargs cp -av –target-directory=​ /home/backup/ –parents # Will locate and copy all files with .txt extension from origin to destiny.
  5. :~$ mkisofs /dev/cdrom > file.iso​ # Will create an ISO image from a CD.
  6. :~$ mkisofs /dev/cdrom | gzip > fileISO.gz​ # Will create a compressed image ISO from a CD.
  7. :~$ cdrecord -v dev=/dev/cdrom file.iso​ # Will burn a CD with an ISO image.
  8. :~$ gzip -dc fileISO.gz | ​ cdrecord dev=/dev/cdrom # Will burn a CD with a compressed iso image.
  9. :~$ tar -Puf backup.tar /home/user​ # Will create an incremental backup.
  10. :~$ tar -czvf compressed.tar.gz /folder/to/compress/ # Will compress a folder tar.gz format.
  11. :~$ tar -xzvf file.tar.gz # Will decompress a compressed file with tar.gz extension.
  12. :~$ tar -cvf compressed.tar /dir/to/compress/ # Will compress folder with tar format.
  13. :~$ tar -xvf compressed.tar # Decompress (or untar) a compressed file with tar format.
  14. :~$ gzip -9 index.php # Will compress a file using gzip format.
  15. :~$ gzip -d index.php.gz # Decompress a file with gzip format.
  16. :~$ zip file.zip /folder # Will compress a folder using zip format.
  17. :~$ unzip file.zip # Unzip a compressed file with zip format.
  18. :~$ mysqldump –user=$user –password=$password –host=$host $db_name > /var/backups/mysql_dumps/$version-$db_name-$date.sql # Gets a dump database file with .sql extension.

3- Groups, users and permissions (15 examples)

3.1- Users and Groups (8 examples)

  1. :~$ whoami # Return what is your current user.
  2. :~$ groupadd new_group_name # Creates a new user group in the system.
  3. :~$ useradd new_user_name #Creates a new user in the system.
  4. :~$ useradd -c “Comment to ​new_user_name” -g group_name -d /path/new/home -s /bin/bash​ new_user # Creates new user with more data.
  5. :~$ cat /etc/passwd # Show all the users created in the system.
  6. :~$ cat /etc/group​ # Show all the groups from the system.
  7. :~$ passwd ​ user_name # Allows change the password for the user.
  8. :~$ chage -E ​ 2020-12-31 user_name # Set expiration date for a user’s password.

3.2- Permissions and ownership (7 examples)

  1. :~$ chown user filename # Will change the owner of the file.
  2. :~$ chown -R user:group foldername # Changes user and group for a whole folder in recursive mode.
  3. :~$ chmod -R 775 foldername # Gives permissions 775 to a folder, recursive.
  4. :~$ ls -la / ll # Shows a list with all the content in a location, with permissions.
  5. :~$ lsattr # Will show all the special attributes of the content.
  6. :~$ chattr +i filename # Convert the file as inmutable. Can’t be deleted o altered.
  7. :~$ chattr +s filename # Allows a file can be deleted in a safe way.

4- Managing packages (26 examples)

Packages are the basic resource in order to install apps and other tools.

4.1- .DEB -Debian, Ubuntu, Mint, and other derivatives- (4 examples)

  1. :~$ dpkg -i ​ package.deb​ # Will install or will update a .deb package.
  2. :~$ dpkg -r ​ package.deb​ # Will delete a certain .deb package in your system.
  3. :~$ dpkg -s ​ package.deb​ # Will show information about the .deb package installed in your system.
  4. :~$ dpkg -l​ # Will show all the .deb packages installed in your sistem.

4.2- Using APT (8 examples)

  1. :~$ apt install ​ packagename # Will install or update a certaing package .deb.
  2. :~$ apt-cdrom install packagename​ # Will install or update a .deb package from a cdrom.
  3. :~$ apt update​ # Will update the installed packages list.
  4. :~$ apt upgrade​ # Will update all the instaled packages.
  5. :~$ apt remove packagename # Will delete the selected .deb package.
  6. :~$ apt list # Gets a list of packages with status.
  7. :~$ apt clean​ # Will clean the cache from the downloaded packages.
  8. :~$ apt search php7.3 | grep php7.3 # Looking for a package with a certain version.

4.3- .RPM -Red Hat, Fedora, SUSE, etc- (10 examples)

  1. :~$ rpm -ivh package.rpm​ # Will install a rpm package.
  2. :~$ rpm -ivh –nodeeps paquete.rpm​ # Will install a rpm package ignoring dependencies.
  3. :~$ rpm -U package.rpm​ # Will update a rpm package without change configurations in files.
  4. :~$ rpm -F package.rpm​ # Will update a certain rpm package if is installed.
  5. :~$ rpm -e ​ package.rpm​ # Will delete the marked rpm package.
  6. :~$ rpm -qa​ # Will show all the rpm packages installed in the system.
  7. :~$ rpm -qi ​package # Will show info about a specific package.
  8. :~$ rpm -q ​ package​ –changelog​ # Will show the revision history of a rpm package.
  9. :~$ rpm –checksig ​ package.rpm​ # Will verify the integrity of a rpm package.
  10. :~$ rpm -qa gpg-pubkey​ # Will verify the integrity of all the rpm installed packages.

4.4- YUM -Red Hat, Fedora and others- (4 examples)

  1. :~$ yum install package​ # Will download and install a certain rpm package.
  2. :~$ yum localinstall ​package.rpm​ # Will install a rpm package resolving all its dependencies.
  3. :~$ yum update package.rpm​ # Will update all the rpm packages in the system.
  4. :~$ yum update package # Will update a certain rpm package.

5- Working in projects (57 examples)

We are in the most specific section related to Drupal and the thousand daily tasks. These commands will make it much easier for you (in addition to all the previous ones) to perform the usual tasks in Drupal-based projects.

5.1- Reviewing history in prompt (3 examples)

  1. :~$ history 7 # Will show the marked amount of the most recent commands.
  2. :~$ !! # Executes the most recent command, the last one.
  3. :~$ history |grep ping # Get the output from history command and piping it as an input to grep command for locating a specific command.

5.2- Applying style reviews with phpcs (6 examples)

PHPCS is a Code Sniffer, ready for review coding standards in your code. github.com/PHP_CodeSniffer.

  1. :~$ phpcs -i # Will show all the installed coding standards.
  2. :~$ phpcs –standard=Drupal -e # Will list all the sniffs inside a coding standard.
  3. :~$ phpcs –standard=Drupal ./HumanstxtSettingsForm.php #​ Review a php file under phpcs.
  4. :~$ phpcs –standard=DrupalPractice ./HumanstxtSettingsForm.php # Same but with another coding standard.
  5. :~$ phpcs –report=diff /path/to/code # Will generate a patch file from a style diff.
  6. :~$ phpcbf /path/to/code # Will repair the file.

5.3- Version Control with Git (17 examples)

Git is the most important tool for version control in software, so you have to learn the basics of git. Learn Git here: git-scm.com/docs and here: git-scm.com/book.

  1. :~$ git remote show origin #Returns the data from the remote repository named ‘origin’.
  2. :~$ git pull origin your_branch –allow-unrelated-histories # Repairs fatal: refusing to merge unrelated histories.
  3. :~$ git diff stash@{0} your_branch > getting_your_patch.txt # Getting a patch between recent stashed code and last status of your_branch.
  4. :~$ git log –date=rfc -n 20 –oneline –format=”%h %<(70,trunc)%s - [%<(20,trunc)%aN] - %ad” # Getting the last 20 commits showing its hash, message, author and time.
  5. :~$ git log –oneline –decorate –graph –all # Show all the branches in the tree format history with pointers.
  6. :~$ git branch –sort=-committerdate #Sort branches by commit date.
  7. :~$ git checkout - # Move to a previous branch.
  8. :~$ git checkout @{-N} # Move to N-previous branch.
  9. :~$ git status -sb # Getting info from a repository using a shorten output.
  10. :~$ git config –get remote.origin.url # Getting info about the remote URL direction of a repository.
  11. :~$ git -C /path/your/project/ log # Getting info from a repository without being in it.
  12. :~$ git -C /path/your/project/ status # Execute some git commands also without being in it.
  13. :~$ git checkout develop README.md # Execute merge over changes in the same file from different branches.
  14. :~$ git diff master develop README.md # Show all the diff between the same file from two different branches.
  15. :~$ git push origin ccccc:master # With commits a, b, c, d. Will push c, b, a, to remote.
  16. :~$ git cherry-pick af0w30b # Will apply and commit changes from another location/branch.
  17. :~$ git cherry-pick af0w30b –no-commit # Same as former, will apply but don’t commit changes from another location/branch.

Docker Engine is the de-facto standard for containers. See more commands about Docker Engine and DDEV at www.therussianlullaby.com/docker-cheatsheet/.

  1. :~$ systemctl is-active docker # Checks if Docker is active or not.
  2. :~$ docker version # Gets a whole report about your Docker Engine installation.
  3. :~$ docker image ls # Gets a list of all the images.
  4. :~$ docker rmi $(docker image ls -q) # Deletes images by its ID.
  5. :~$ docker image prune # Removes unusued images.
  6. :~$ docker exec idcontainer unixcommand # Executes a command inside a running container.
  7. :~$ docker exec -it IDCONTAINER /bin/bash # Connect to the Prompt of a Container.
  8. :~$ docker cp db/dump.sql IDCONTAINER:/tmp/dump.sql # Copying files from Local to Remote Docker Container.
  9. :~$ docker cp IDCONTAINER:/tmp/dump_test.sql ./db # Copying files from Docker Container to local.
  10. :~$ docker system prune -f # Remove unusued data and clean the Docker System.
  11. :~$ docker stats –all –format “table \t\t” # Show stats about the running containers with formatted output.
  12. :~$ docker-compose logs -t -f ALIAS # Get the Docker container log with direct connection.
  13. :~$ docker-compose stop && docker-compose up –build –force-recreate # Stops, rebuilds and relaunchs.
  14. :~$ docker service create –replicas 3 –name my-service -p 8080:80 –network net-end my-app # Swarm: Creates new service with params.
  15. :~$ docker service scale my-service=6 # Swarm: updating service up to six nodes.

5.5- Building projects using DDEV (8 examples)

DDEV is a Containerization based tool, nowadays in the basic stack for Drupal projects. With DDEV-Local you can deploy fast Drupal projects.
See more information about DDEV:

  1. :~$ ddev version # Check your DDEV installed version.
  2. :~$ ddev launch # Launch in browser your Drupal site.
  3. :~$ ddev logs # Show logs mixing nginx and php-fpm. Works in a stopped container.
  4. :~$ ddev logs -f # Same as former, but following logs in real-time.
  5. :~$ ddev ssh # Connecting to the web container with ssh.
  6. :~$ ddev ssh -s db # Connecting to the database container using ssh.
  7. :~$ ddev export-db –gzip=false > /temp/db.sql # Returns a dump file in the selected folder.
  8. :~$ ddev import-db –src=./backups/dump_02062020.sql.gz # Load a compressed dump file in the default DDEV database called ‘db’.

5.6- Drupal CLI using Drush or Drupal Console (13 examples)

Drush

Drush (Drupal Shell) is the so called “Swiss army knife”, a command-line tool (CLI) for Drupal. You can use Drush from your prompt and executing thousands of options, you’ll be able to work with Drupal in a very interactive way.
See more information about Drush:

  1. :~$ drush si –site-name=Example-Drupal –account-name=admin –account-pass=admin -y # Executes the installation process of a downloaded (and unzipped) Drupal codebase.
  2. :~$ drush -l sitename cr # Clear cache only for a site in a multisite Drupal installation.
  3. :~$ drush migrate-fields-source your_migration_file # Show the available fields in Drupal Migrations.
  4. :~$ drush migrate-import your_migration_file –limit=”50 items” –feedback=”5 items” # Executes a migration process but only transferring 50 items with info every 5 items.
  5. :~$ drush config-delete ‘module.config_settings’ # Deletes Configuration Objects.
  6. :~$ drush cim –partial –source=only-few-configs/ # Importing partial configuration files.
  7. :~$ drush watchdog:show –count=20 # Getting the last 20 problems from a Drupal site.
  8. :~$ drush sql-query “SELECT CONVERT(value USING utf8) FROM key_value WHERE name = ‘the_module_name’” # Gets the info about the current version of a module.
  9. :~$ drush views:analyze # Gets the total account of Views and its status..

Drupal Console

Drupal Console is a CLI tool to generate boilerplate code, making scaffolding and interact with a Drupal installation. Now, seems to have a new updated version ready for Drupal 9 and suitable with Symfony 4.4.
See more information about Drupal Console:

  1. :~$ drupal cect application –module=”managing_activities” –optional-config –remove-uuid –remove-config-hash # Export the config of an specific content type as optional.

  2. :~$ drupal debug:container # Gets a list of available services in a Drupal installation.

  3. :~$ drupal debug:event # Gets a list of available events in your Drupal installation.

  4. :~$ drupal generate:module –module=”modulename” –machine-name=”modulename” –module-path=”/modules/custom” # Generates a new Drupal custom module.


Written on September 20, 2020