When using a Linux operating system, you can type various commands on the command prompt in the terminal window to perform different operations. Understanding the most basic Linux commands will allow you to navigate directories, mount drives, copy files, change permissions, administer users, and so on. We’ll take a look at these commands in this section.
Listing Files and Directories
ls |
List files and directories |
ls -a |
List all files and directories |
ls -l |
Detailed list of files and directories |
mkdir <dir-name> |
Create directory |
cd <dir-name> |
Change to directory |
cd / |
Change to root directory |
cd ~ |
Change to home-directory |
cd .. |
Change to parent directory |
find <name> |
Find file <name> (use *) |
pwd |
Display the path of the current dir |
Working with Files
cp <file> <destination> |
Copy file to destination |
mv <file> <destination> |
Move file to destination |
rm <file> |
Delete file |
rmdir <dir> |
Delete directory |
more <file> |
Displays file a page at a time |
head <file> |
Displays first few lines of file |
tail <file> |
Displays last few lines of file |
grep ‘keyword’ <file> |
Search file for keyword |
zip -r <archive> * |
Zip files & folders in current directory |
zip <archive-name> <files> |
Compress files into zip archive |
unzip <file-name> |
Uncompress zipped file |
nano <file-name> |
Text editor |
tree |
Display directory tree |
File Permissions
chmod [options] <file> |
Change access rights for a file or directory
Options:
user, group, other
Eg: chmod 744
- 4 stands for “read”,
- 2 stands for “write”,
- 1 stands for “execute”, and
- 0 stands for “no permission.”
User: 7 (4+2+1) (read, write, execute)
Group: 4 (read)
Other: 4 (read) |
sudo chown <user> <file> |
Change file/dir ownership |
Users
sudo <command> |
Execute command with root privileges |
su <username> |
Switch user |
sudo useradd <username> |
Add new user |
sudo groupadd <groupname> |
Add new user group |
sudo usermod -g <group> <user> |
Change user’s group |
sudo passwd <username> |
Change password of user |
whoami |
Show user currently logged in |
Network
netstat |
Displays network state |
ping |
Check host is online |
sftp [user-name]@[server] |
Secure ftp connection to server |
hostname |
Display hostname or machine name |
ssh [user-name]@[server] |
Open a SSH connection to server |
System
sudo shutdown – –poweroff |
Show down machine |
sudo reboot |
Reboot machine |
man <command> |
Get manual page on command |
sudo mount <dev> <dir> |
Mount device
Eg: sudo mount /dev/sda2 ~/media |
wget <package-url> |
Download and install package |
clear |
Clear terminal window |
lsblk |
Show devices connected to your system |
sudo parted /dev/<drive> |
Partition drive (sda)
- mkpart primary ext4 1MiB <size>
- type print to display all partitions
- rm <partition> to delete partition
|
sudo mkfs.ext4 /dev/<partition> |
Create file system on partition |
Return to resources