Information, Navigation, and Management Commands
Overview
What is a shell?
User interface for running commands
Interactive language
Scripting language, can be used to automate tasks
Default shell: Bash. Other shells: sh, ksh, tcsh, zsh, and fish. Check shell with
printenv SHELL
This returns the path to the default shell program
Shell command applications
Getting information
whoam
: User nameid
: User ID and group IDuname
: OS nameps
: Running processestop
: Resource usagedf
: Mounted file systemsman
: Reference manualdate
: Today’s date
Navigating and working with files and directories
Files
- Files
cp
: Copy fiilemv
: Change file name or pathrm
: Remove filetouch
: Create empty file, update file timestampchmod
: Change/modify file permissionsws
: Get count of lines, words, characters in filegrep
: Return lines in file matching pattern
- Directory
ls
: List files and directoriesfind
: Fined files in directory treepwd
: Get present working directorymkdir
: Make directorycd
: Change directoryrmdir
: Remove directory
Printing file and string contents
cat
: Print file contentsmore
: Print file contents page-by-pagehead
: Print first N lines of filetail
: print last N lines of fileecho
: Print string or variable value
File compression and archiving
tar
: Archive a set of fileszip
: Compress a set of filesunzip
: Extract files from a compressed zip archive
Performing network operations
hostname
: Print hostnameping
: Send packts to URL and print responseifconfig
: Display or configure system network interfacescurl
: Display contents of file at a URLwget
: Download file from URL
Monitoring performance and status of the system, its components and applications
Running batch jobs, such as ETL operations.
Informational Command
- Display user information
- Verify identity or identify user account
Command | Usage |
---|---|
whoami | Return user name |
id | User or Group IDid -u returns the numerical ID of the userid -u -n returns the name corresponding to the numerical user ID |
uname (Unix name) | Returns OS information |
df (disk free) | Show disk usage to monitor disk usage or check space |
ps (process status) | Monitor or manage processes |
top (table of processes) | Task manger: Monitor system performance and reosurce usage |
echo | Print string or variable value |
date | Display system date and time |
man <command> (manual) | Show manual for any command |
is a free and open-source collaborative documentation effort, which provides documentation that is more accessible than the traditional man pages, along with practical examples.
Example
ls
File and Directory Navigation Commands
pwd
, cd
ls
find
: Find files in directory tress.
Example:
iname
: case insensitive
File and Directory Management Commands
mkdir
rm
-i
option, which creates a prompt to ask for confirmation before every deletion.rmdir
: Remove empty directory
touch
: Create empty file, update file date
cp
: Copy file or directory (-r
) to destination
mv
: Move file or directory
when the source and target directories are the same, you can use
mv
to rename a file. Example: usemv
to renameusers.txt
touser-info.txt
by entering the following command:mv users.txt user-info.txt
chmod
(change mode): Change file permissions
Specify which permissions to change with a combination of the following characters:
Option Description r
,w
,x
Permissions: read, write, and execute u
,g
,o
User categories: user, group, and all others +
,-
Operations: grant and revoke Example
Managing File Permissions and Ownership
Three possible levels of file ownership in Linux: user, group, and other.
- Whoever creates a file, namely the user at the time of creation, becomes the owner of that file by default.
- A group of users can also share ownership of a file.
- The other category essentially refers anyone in the universe with access to your Linux machine - careful when assigning ownership permission to this level!
Only an official owner of a file is allowed to change its permissions. This means that only owners can decide who can read the file, write to it, or execute it.
Viewing file permissions
Example:
$ echo "Who can read this file?" > my_new_file
$ more my_new_file
Who can read this file?
$ ls -l my_new_file
-rw-r--r-- 1 theia users 25 Dec 22 17:47 x
Here we’ve echoed the string "Who can read this file?"
into a new file called my_new_file
. The next line uses the more
command to print the contents of the new file.
Finally, the ls
command with the -l
option displays the file’s (default) permissions: rw-r--r--
.
- The first three characters (
rw-
) define the user permissions. You, being the user, have the permissionrw-
, which means you have read and write permissions by default, but do not have execution permissions. Otherwise there would be anx
in place of the last-
. - The next three (
r--
) the group pemissions. The final three (r--
) the other permissions.
Looking at the entire line, rw-r--r--
, you can see that anyone can read the file, nobody can execute it, and you are the only user that can write to it.
-
at the very beginning of the line in the terminal means that the permissions are referring to a file. If you were getting the permissions to a directory, you would see a d
in the front for “directory”.Directory permissions
The permissions for directories are similar but distinct for files. Though directories use the same rwx
format, the symbols have slightly different meanings.
The following table illustrates the meanings of each permission for directories:
Directory Permission | Permissible action(s) |
---|---|
r | List directory contents using ls command |
w | Add or remove files or directories |
x | Enter directory using cd command |
Making a file private
You can revoke read permissions from your group and all other users by using the chmod
command.
Example:
$ chmod go-r my_new_file
$ ls -l my_new_file
-rw------- 1 theia users 24 Dec 22 18:49 my_new_file
In the chmod
command, go-r
is the permission change to be applied, which in this case means removing for the group (g
) and others (o
) the read (r
) permission.
Executable file
A Linux file is executable if it contains instructions that can be directly interpreted by the operating system. Basically, an exectuable file is a ready-to-run program. They’re also referred to as binaries or executables.
Script is a particular kind of executable. shell scripting, or more specifically Bash scripting, which is writing scripts in Bash (born-again shell), a very popular shell scripting language. A shell script is a plain text file that can be interpreted by a shell.
Formally speaking, for a text file to be considered an executable shell script for a given user, it needs to have two things:
- Execute permissions set for that user
- A directive, called a “shebang”, in its first line to declare itself to the operating system as a binary