Be a Power using the Command Line interface in Ubuntu 22.04
Understand your computer from the command line in Ubuntu 22.04.
Table of contents
- File Management Commands
- File/ Directory permissions and groups
- Linux Utilities
- User Identification and who is in Linux
- Process related information
- File Manipulation
- File/Directory details
- Linux Distribution name and version
- Getting Ubuntu System Information
- File compression with 'tar' command
- Services in Ubuntu
- Secure Shell (SSH)
- Modifying users
Lets take a deep dive in our Ubuntu terminal and understand the various commands we can use to navigate through our computer, access the files and processes running in the computer. Prerequisites you need an Ubuntu environment and you are good to follow through this article.
A shell is a command line interpreter that executes user commands. In Ubuntu the default shell is Bash (Bourne Again Shell). We have other shells such as sh, zsh, ksh.
To open your terminal :
CTRL + ALT + T
File Management Commands
Where you are in your terminal is called current working directory.
pwd
Navigating to the last directory you were in
cd
Navigate to current users home directory
cd ~
Go to parent directory of current directory
cd ..
List files and directories in current directory in long format
ls -l
List all files including hidden files
ls -a
List file sizes in human readable format
ls -lh
List all sub-directories, generate tree representation of file system from current directory
ls -lR
File/Directory create, copy and remove commands
cp -p source destination
To copy file from source to destination. -p for preservation preserves original attributes of a file while copying like permission, owner, timestamp
cp -R source_dir destination_dir
Copy source directory to specified destination recursively.
mv ./file1 to ./file2
moves/ renames the file in your directory.
To asks for confirmation before file removal in your machine
rm -i <your filename>
To remove the directory dir-name recursively, ignoring existent files and never prompt for anything.
rm -rf <your dir-name>
To remove empty directory
rmdir <dir-name>
To create a filename
touch <your filename>
To create a directory dir-name
mkdir dir-name
File/ Directory permissions and groups
We have three persons in our system: user, group, other denoted by u, g, o respectively. The permissions that can be executed on our files are: read, write , execute denoted by r, w, x respectively
- used to add permissions
chmod <specification> <filename>
To change file permissions
To add read permission for the group and group on the filename
chmod go=+r <your filename>
To allow users to read, write or execute your filename.
chmod a +rwx <your filename>
To change primary group ownership of directory to group grp_owner recursively. To change group ownership of a directory and everything within the directory.
chgrp -R grp_owner <dir-name>
Linux Utilities
To display the user manual of any command that we can run on the terminal.
man <command>
To output all software whose man pages contain keyword
man -k <editor>
To output a list of all installed packages on a Debian-based system.
dpkg -l
Package name, will list out the files installed and path details for a given package on Debian
dpkg -L
To return description of all available packages
less /var/lib/dpkg/available
User Identification and who is in Linux
To display hostname of the system
hostname
To display fully qualified domain name of the system
hostname -f
The username of the users logged in as a user
whoami
To list all users logged inas a user. Display system status, time, duration, users logged in on system
who
To show all bad logins attempt into the system
lastb
To change permissions, to read, write, execute of a file or directory
chmod
Process related information
List all processes sorted by current system system resource usage. Display a continually updated display of processes.
top
List all of processes and commands root is running
ps
List all processes and commands root is running
ps -u root
List all processes by all users on the current system
ps aux
Searching for files by pattern in name/ contents
To check files by name
find /var/www -name
To display print all lines containing the pattern font in the specified file. Finding containing text.
grep font/var/www/style.css
When grep is matching multiple files, it prefixes the matched lines with file names.
File Manipulation
The files and directories(folders) are heart of Linux. To create a file
touch <myfile>
To rename a file
mv <yourfile> <yourfirstfile>
To view contents of a file
cat <yourfitstfile>
To view content of a file with pager
less <yourfirstfile>
To see files you are in working directory
ls
To create an empty directory
mkdir <yourfirstdirectory>
To delete an empty directory
rmdir <yourfirstfirstdirectory>
To delete a non-empty directory
rm -rf <yourfirstdirectory>
To delete a file
rm <yourfirstfile>
File/Directory details
ls command can be used together to show more information
To show file permission, size, last modified date
ls -l
permission are in format of drwxrwxrwx;
- d for directory
- rwx - first for user permission over the file.
to change rights you can chmod value : r - read, represented by a value of 4 w - file modified, represented by a value of 2 x - executed, represented by a value of 1
owner has rwx = 4+2+1=7 group has r-x = 4+0+1=5 other has r-x = 4+0+1=5
for example to modify contents in a work_planned directory
chmod 755 work_planned
To view all files including the hidden files use:
ls -a
Linux Distribution name and version
To detect the Debian-based distribution you are working in:
cat /etc/issue
To print information about the current system
uname
To print all information of our running Linux kernel
uname -a
To find your Ubuntu OS name and release number
cat /etc/*release
Changing the default shell
Mostly Ubuntu comes with Bash Shell, however as we discovered earlier we also have other shells. To change shell we use the command chsh (change shell). To view all shells installed on the machine run the command:
chsh -l
To print usage message, run the command:
chsh -h
Creating your own command alias
In case you don't want to type long commands in your bash shell, you can easily create your own alias then modify a file called bash_aliases in home folder. alias command_alias = 'actual command' For example alias install = 'sudo apt-get -y install' This means you use install in a terminal, interpreted by bash as sudo apt-get -y install. However I would discourage using this in other people's systems to avoid confusions.
Locate a file on the system
Use locate command
for example:
locate school.js
To check disk space
It is a good practice to know about your disk usage. du command, summarizes disk usage of set of files, recursively for all directories To see the output in a human readable format
du -sh
To use du command on the root directory to find application/service consuming disk space:
sudo du .sch /.[!.]* /*
Getting Ubuntu System Information
One can know he details and statistics about CPU, memory, network and Disk(I/O operations)
For Network run:
netstat
For Disk run:
iostat
For Memory run:
vmstat
For CPU run:
mpstat
The above images show the output for our netstat, iostat, vmstat, mpstat commands, however I displayed only the top details only.
Process monitoring and Information Gathering
ps (process status) command is used to provide information about currently running processes, including their process identification numbers (pids) To list process by hieracy:
ps -e -o pid
File compression with 'tar' command
- -- create or -c, create a new archive
- -- extract or - x, extract files from an archive
- -- list or -t, list contents of an archive
- -- file or -f, use archive file
- -- verbose or -v, verbosely list files processed
compression options
- --auto-compress or -a, use archive suffix to determine compression
- --gzip or -z, filter the archive through gzip
- --bzip2 or -j, filter the archive through bzip2
- --xz or -J, filter the archive through xz
Compress Folder
tar -cf /your-archive.tar/your-folder/ - simple archive folder
tar -cvf /your-archive.tar/your-folder/ - verbose output shows files and directories added to archive
tar -czf /your-archive.tar/your-folder/ - archiving a folder compressed 'gzip'
Extract a folder from an archive
tar -xf archive-name.tar -C /directory/destination
To list archive content without extracting. The option -t used for listing:
tar -tf archive.tar.gz
Services in Ubuntu
To list running services in Ubuntu, to see state of services controlled by system run.:
service --status -all
Where + - indicates service is running and - indicates that service is stopped.
Service Management - Systemd
Listing services, to check the failed and running services:
systemctl --failed
systemctl
Managing services at runtime
To start a service
systemctl start [service-name]
To stop a service
systemctl stop [service-name]
To restart a service
systemctl restart [service-name]
To show current status of a service
systemctl status [service-name]
Masking services, makes its hard to start a service by mistake
systemctl status [service-name]
Restarting systemd
systemctl daemon-reload
Troubleshooting a problem with a service, to check basic status information and recent error logged.
systemctl status [service-name]
Secure Shell (SSH)
SSH is used to remotely access a server from client over an encrypted connection. Open SSH client is mostly used in Ubuntu and other GNU/Linux distros. Connecting to a remote server:
ssh -p port user@server-address
ssh -p 22 denis@exporter.servers.com
port - listening ssh port of server user - existing user on server with ssh privileges server address - IP/Domain of server
Modifying users
It is crucial to keep track of users who are in the system. To run this operations you should have access to root privileges.
username - the name of the user. Do not use capital letters, dots, end it with a dash. Do not include colons, no special characters, cannot start with a number.
To set your own password run:
passwd
Setting another user's password run:
passwd <theusername>
adding a user in the system:
useradd <theusername>
Removing a user run:
userdel <theusername>
Listing groups that the current user is in:
groups
To remove a user and its home folder run:
userdel -r username
Listing groups a user is in run:
groups username
To get more details for the users run:
id username
That's it for our article today, we navigated from the basic commands in Linux from our Ubuntu machine to the advanced concepts. This commands will also work in other lower versions of Ubuntu and other Debian Linux distros.
Share you comments about this article.