Saturday, March 5, 2016

Basic Linux Commands

This blog article contains list of Basic Linux Commands mostly for the beginners. With a little introduction, a terminal is the command interpreter also the Linux shell, it is where Linux commands are typed to be executed.

A Linux kernel is the heart of a Linux system while a Shell Script is a list of commands stored in a file that the shell executes non-interactively.

Their are different types of Linux command shells, for example;
  •   bourne shell ( $ character prompt)
  •   c shell (% character prompt)

  1. Simple commands - Example: who
  2. Complex commands - Example: who am i
  3. Compound commands - Example: date ; who am i ;

When you open the terminal, the Home Directory is the directory in which you start out after you log in.
NOTE: In Linux systems, it is case sensitive, Home, hOme,HOME,homE are three different files or folders depending on the scenario.

su (super user) -allows a user to automatically log in as root/super user.
sudo (super user do) -allows a permitted user to execute a command as the superuser or as root. According to the character prompt of your shell, it changes to # character prompt.

NOTE: It is not advisable to use su for day to day use since it can result to serious errors when accidentally used hence sudo is considered safer than su.

cal Displays calendar of present month and year.
cal 1990 Displays all the months of the year 1990.

date Prints current date and time.

cd Change directory command, it changes the working directory.
cd Desktop Changes the directory to Desktop.

find Allows one to search for a file in a given directory.
find file_name

who It displays who is logged on, in which tty and time the user logged on.

whoami Displays your username associated with the current user ID

uptime Displays the time the system has been up/on.

exit Exits the user from the terminal screen.

clear This command clears the visible area of the terminal screen.

du Shows how much space each file takes up.

top Provides a quick overview of the currently running processes, number of users logged in and the uptime.

kill is used to terminate processes that mostly cannot be terminated in a normal way. By executing it, you specify the respective process ID displayed from the top command.
kill 175 Kills process ID=175.

passwd Allows users to change their own password.

free Displays the amount of memory used and free system memory.

ifconfig Used to configure the kernel network interfaces.

ping Allows a user to verify that a particular IP address accepts requests, used to test connectivity and determine the response time

w Prints the current system users.

shutdown Shuts down the computer.
shutdown -r Reboots of the computer after shutdown.
shutdown -h
Halts the computer after shutdown
shutdown -c Cancels the shutdown process

tar puts file(s) into an archive
tar cvzf file_name.tar.gz /home/My_archives The command will create a compressed file for the directory /home/My_archives.
-f writes the output to a file and not the screen as is usually the case.
-c creates new tar archive
-r adds files to existing archive
-t outputs the contents of an archive
-u add files, but only if they are newer than the files already in the archive
-x extracts files from an archive
-j packs the resulting archive with gzip
-v lists the files processed

Listing Files
ls List files of the current directory you are in.
ls -a view hidden files
ls -F shows files and directories
ls -1 produces a listing
ls -l list all files with permissions
You can use all the ls arguments at a go i.e;
ls -aF1l
Stands for list block devices, prints block devices by their assigned name and also Usb devices.

Views the contents of a file.
cat -b file_name numbers the output minus the blank spaces.
cat -n file_name numbers the outputs.

wc Counts the words, lines, and characters in a file.
wc -lwc file_name
wc -l file_name counts lines
wc -w file_name counts words
wc -c file_name counts characters

Copying Files
cp command
cp file_name source destination
cp source destination Directory/
Copies a file to a given directory.

Renaming Files
mv command
mv file_name new file_name

Removing Files
rm command
rm file_name
rm -i file_name
Interactive mode for removing files.

Creating New Files
You can use different text editors to create new files like leafpad, gedit, nano etc depending with your Linux distribution. For this blog am using vim.
Ensure you have vim installed;
Type vim to open the terminal enabled editor
press 'a' to insert text and 'Esc' key to get out of insert mode
 n goes to the next file in the file search
:e filename switches to another file or to open a filename after you are in vim intro-page
:u undoes the last edit command
:q quits vim
:q! forces close of vim
saves and quit
:x saves the modified files and exits the editor
:w file_name save as (to new file_name)

Directories and their Manipulation
pwd Prints the working directory.
mkdir directory_name Creates a new directory
mkdir -p notes/linux/commands/permissions Creates parent directories notes/linux/commands if they don't exist.
cp -r source destination Copies a directory.
cp -r docs/book docs/school work/src /mnt/zip Copies multiple dir book,school, src to the zip directory.
source destination Moves file and directories.
rmdir directory_name Removes an empty directory. 
rm -r directory_name Removes a directory will all its contents.

User_Groups and Permissions
sudo adduser username adds a user
sudo userdel username deletes a user

/etc/passwd Stores users information. 
/etc/group Stores all information about groups.

sudo passwd username Changes user password. 
sudo groupadd groupname Adds a group.
sudo groupdel groupname Deletes a group.
sudo adduser username groupname Adds a user to a group.
sudo deluser username groupname Removes a user from a group.
sudo chmod 777 foldername Changes the permissions of a folder.

File or folder permissions can be changed with three permissions(read,write and execute) each with a unique number identified with it.
4 -read permission (r)
2 -write permission (w)
1 -execute permission (x)

With basic math 4+2+1=7(All the 3 permissions equals full permission).

777, 775, 770, 700 etc
1st instance of the three figure - Owners permission to a file or folder.
2nd instance of the three figure - Group permission to file or folder.
3rd instance of the three figure - Everybody else/others permission to a file/folder.

775 owner and group full permission while others read and execute permissions only to a file or folder
777 owner, group and others full permissions of read, write and execute to a file or folder

sudo chmod 777 filename/foldername Gives owner, group and everybody else all full permissions of read, write and execute to a file or folder.

sudo chmod 775 filename/foldername Gives owner, group full permissions of read, write and execute while everybody else read and execute permission only to a file or folder.

sudo chmod 777 foldername -R (for recursive folders inside)

sudo chwon -R username file_name/folder_name Changes the user ownership of a folder/file. sudo chgrp -R groupname file/folder Changes the group ownership of a folder/file.

Man Pages
Every version of Linux comes with an extensive collection of help pages called man pages (short for manual pages ). The man pages are the authoritative source about your Linux system.
They contain complete information about both the kernel and all the utilities. If you type;
man ls
It brings out a list of information about the ls commands and arguments that go along with it.

Happy learning.

[root@e13olf]# exit

No comments:

Post a Comment