Code Monkey home page Code Monkey logo

Basic Commands

Display Linux system information

#uname -a

alt text

Display kernel release information

#uname -r

alt text

Show which version of redhat installed

# cat /etc/redhat-release

alt text

Show how long the system has been running + load

# uptime

alt text

Show system host name

# hostname

alt text

Display the IP addresses of the host

# hostname -I

alt text

Show system reboot history

# last reboot

alt text

Show the current date and time

# date

alt text

Show this month's calendar

# cal

alt text

You can see your previously run command by executing 'history' command

# history

alt text

Display who is online

# w

alt text

Who you are logged in as

# whoami

alt text

Navigating Path

The pwd command displays the full path name of the current location, which helps determine appropriate syntax for reaching files using relative path names.

# pwd

alt text

The ls command lists directory contents for the specified directory, or if no directory is given for the current directory. as in if I enter "ls /etc" it will display the files and directories under /etc directory

# ls /etc

alt text

Use the cd command to change directories. As example if you run "cd /etc" it will change the directory to /etc

# cd /etc

alt text

Use the cd command to change working directory to logged in user home directory. "cd" and "cd ~" both command will change the working directory to the logged in users home dorectoy.

# cd 
# cd ~

alt text

alt text

use the cd command with .. to change the working directory to parent directory. It works like the the up button of windows explorer. By default every users home directory situated in /home directory so if we execute the command "cd .." from any users directory it will take you the the directory /home.

# cd ..

alt text

use the cd command with - to change the working or current directory to previous working directory. This means if I change the directory to /etc from /home and then I executed "cd -" it will take back you to /home

# cd -

alt text

The ls command has multiple options for displaying attribute on files. The most common and useful are –l (long listing format), -a (all files includes hidden files) and –R (recursive)

# ls -l
# ls -la
# ls -R

alt text

alt text

alt text

File and Directory Management

use mkdir command to create directory

# mkdir dir
# ls

alt text

The mkdir command creates one or more directories or subdirectories, generating errors if the filename already exists or when attempting to create a directory in a parent directory that doesn’t exist. The –p parent option creates missing parent directories for the requested destination.

# mkdir dir2/subdir2 
# mkdir dir2/subdir2 -p
#ls

alt text

alt text

use touch command to create directory

# touch file1
# ls

alt text

to create multiple command with command

# touch file1 file2 file3
# ls
# touch filename{1..5}
# ls
# touch file.txt /tmp/file.txt
# ls
# ls /tmp

alt text

alt text

alt text

The cp command copies one or more files to become new, independent files. Syntax allows copying an existing file to a new file in the current or another directory, or copying multiple files into another directory. In any destination, new file names must be unique. If the new file name is not unique, the copy command will overwrite the existing file. When copying multiple files with one command, the last argument must be a directory.

# ls
# cp file1 myfile.txt
# ls
# ls /tmp
# cp file1 /tmp/
# ls /tmp

alt text

alt text

The mv command renames files in the same directory, or relocates files to a new directory. File contents remain unchanged. Files moved to a different file system require creating a new file by copying the source file, then deleting the source file.

# ls
# mv file2 file2.txt
# ls
# ls
# mv file3 /tmp/
# ls
# ls /tmp

alt text

alt text

Default syntax for rm deletes files, but not directories. Deleting a directory, and potentially many subdirectories and files bellow it, require the –r recursive option. Using -i will interactively prompt for each deletion. This is essentially the opposite of –f which will force the deletion without prompting the user. There is no command-line undelete feature, nor a trash bin from which to restore.

# ls
# rm filename1
# ls
# ls
# rm -rf dir2
# ls

alt text

alt text

The rmdir directoris only if empty. Removed directories cannot be undelete.

# ls
# rmdir dir
# ls

alt text

Read a file in Linux

To read a file one of the most popular command is "cat". cat followed by by the file name or path will display the file content in your shell

# cat myfile

alt text

To read a file in a reverse direction we can use "tac" command

# tac myfile

alt text

"head" is a command to read first 10 lines of a file you can specify the line number by using -n command followed by the desired line number

# head /etc/passwd
# head -n 3 /etc/passwd

alt text

alt text

"tail" is a command to read last 10 lines of a file you can specify the line number by using -n command followed by the desired line number. and we can use -f option to follow the file continiously, which is very useful to read logs

# tail /etc/passwd
# tail -n 3 /etc/passwd
# tail -f 

alt text

alt text

alt text

"head" is a command which is used for reading big file can't read with single screen and have to scroll down. We can scroll the file downwards by pressing "Enter" or "Space" button. And it will take you out from the file when you scroll it down all the way to the end of the file.

# more /var/log/messages

alt text

"less" is a command which is used for reading big file can't read with single screen and have to scroll up and down. We can scroll the file upwards or downwards by pressing "Up Arrow" or "Down Arrow" button. And you have to press the button "q" to get out from the file.

# less /var/log/messages

User Management

We can add user in our system by using command "useradd" or "adduser" and we check users information with "id" command

# useradd user1
# id user1
# adduser user2
# id user2

alt text

We can add user group in our system by using command "groupadd" and we check group information by reading the file /etc/group

# groupadd mygroup
# tail -n 1 mygroup

alt text

We can change the users' default parameter with certain option added with the command "useradd" when we creating a new user. As like with -u option we can change the user id, -g the primary group, -G secondery group, -s shell, -d home directory of the user and with -c we can add comment for the user. We can use this option separately or at the same command. And we can see the users info by using "getent passwd" commad.

# useradd -g mygroup -G user1 -u 2000 -s /bin/sh -d /var/customuser -c "this is a user with custom parameters" customuser
# getent passwd customuser

alt text

To set a users password we can use the command "passwd".

# passwd user1

alt text

Every user can change their own password by using passwd. Note: only root can change other users password on behalf of them.

# passwd

alt text

With passwd command we can lock and unlock a user by usnig -l and -u option with it

# passwd -l user1
# passwd -u user1

alt text

We can expire a user passwd by using -e option with passwd command

# passwd -e user1

alt text

We can check the passwd aging with the command "chage" with a inclusion of option -l followed by the username

# chage -l user1

alt text

We can change the password aging with inclusion of several option as the minimum days between the password change with option -m, minimum days between the password change -M, warning given by before the password expire -W, make the password inactive -I and Expire the account with -E.

# chage -m 3 -M 30 -W 5 user1
# chage -l user1

alt text

To delete a user we can use the command "userdel". Note: we have to use -r option with userdel otherwise the file created for the user will remains on the system.

# userdel user2
# id user2

alt text

We can use "groupdel" to delete a group

# groupdel group1

alt text

To change a existing users parameter we can use "usermod" with options like with -u option we can change the user id, -g the primary group, -G secondery group, -s shell, -d home directory of the user and with -c we can add comment for the user

# usermod -g user1 -G mygroup -s /bin/bash -u 1005 -c "with changed parameter" customuser
# getent passwd customuser

alt text

FILE PERMISSIONS

Linux permision examples

     PERMISSION      EXAMPLE
     U   G   W
    rwx rwx rwx     chmod 777 filename
    rwx rwx r-x     chmod 775 filename
    rwx r-x r-x     chmod 755 filename
    rw- rw- r--     chmod 664 filename
    rw- r-- r--     chmod 644 filename


    U = User
    G = Group
    W = World

    r = Read
    w = write
    x = execute
    - = no access

	r = 4
	w = 2
    x = 1

We can check a file's or directory's permission or ownership or group ownership by using "ls -l" command. And change the ownership to another user by using "chown" command

# ls -l file
# chown shibli file
# ls -l file

alt text

We can change the group ownership of a file or directory by using "chgrp" command

# ls -l file
# chgrp mygroup file
# ls -l file

alt text

We can change ownership and group ownership with a same command of "chown" in that case we have separate the user and group name by ":"

# ls -l file
# chown root:root file
# ls -l file

alt text

With "chmod" command we can change the permissions of a file as described in the note earlier. by using Numric value :

# ls -l file
# chmod 777 file
# ls -l file
# chmod 666 file
# ls -l file
# chmod 644 file
# ls -l file
# chmod 000 file
# ls -l file

alt text

Or we can change the value specificly

# ls -l file
# chmod u+r file
# ls -l file
# chmod u+w file
# ls -l file
# chmod g+r file
# ls -l file
# chmod o+r file
# ls -l file
# chmod u+x file
# ls -l file
# chmod ug=rw,o=r file
# ls -l file

alt text

"Set user ID" or "SUID" is one of the three special permissions, by using SUID bit we can run a file with its owners permission.

# ls -l file
# chmod u+s file
# ls -l file

alt text

"Set group ID" or "SGID" is one of the three special permissions, by using SGID on a directory we can pass on the group ownership to is child directorys'.

# ls -ld dir
# chmod g+s dir
# ls -ld dir

alt text

Sticky bit normally use for a common directory where everyone has all the permissions but because of the sticky bit one can't change or delete another users file.

# ls -l file
# chmod o+t file
# ls -l file

alt text

shiblijoy's Projects

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.