Skip to content

Instantly share code, notes, and snippets.

@moshiurse
Last active February 19, 2023 07:41
Show Gist options
  • Save moshiurse/9e62a765e181bc0d487b5a3ca211f19f to your computer and use it in GitHub Desktop.
Save moshiurse/9e62a765e181bc0d487b5a3ca211f19f to your computer and use it in GitHub Desktop.

Linux Command Line Primer

Terminal breakdown

what do moshiur@pc:~$ means ?

  • moshiur: The username of the current user
  • pc: The hostname of the server
  • ~: The current directory. In bash, which is the default shell, the ~, or tilde, is a special character that expands to the path of the current user’s home directory; in this case, it represents /home/sammy
  • $: The prompt symbol. This denotes the end of the command prompt, after which the user’s keyboard input will appear

root@webapp:/var/www# Logged in as a root in var/www directory

User setting

Login as root

sudo bash

Get a List of All Users

less /etc/passwd
OR
getent passwd

List of normal users created by sudo access

getent passwd {1000..60000}

Directory & File System

Present working directory

pwd

Change directory

cd /var/www/html (Using Absoulate path)
cd var (Using relative path)

If you run the cd command without any arguments, you will be returned to your current user’s home directory.

cd ~ OR cd

Change directory

cd /var/www/html

Change to the previous directory (Like Stack)

cd -

Change to the root directory

cd /

Change to another user's home directory

cd ~moshiur

Change to Directory having Space

cd 'Dir name with space'
cd Dir\ name\ with\ space

List files and folders

The ls command will print a listing of the current directory’s files and directories.

ls

A Linux command has the following basic syntax:

ls [ Options ] [File]

  • ls -a list all files including hidden file starting with '.'.
  • ls -d list directories - with ' */'.
  • ls -l list with long format - show permissions.
  • ls -F Append indicator (one of */=>@|) to entries.
  • ls -lh This command will show you the file sizes in human readable format.
  • ls -r list in reverse order.
  • ls -i list file's inode(index) number.
  • ls -ltr View Reverse Output Order by Date.
  • ls -t sort by time & date.
  • ls -n It is used to print group ID and owner ID instead of their names.
  • ls -m A list of entries separated by commas should fill the width.
  • ls -g This allows you to exclude the owner and group information columns.
  • ls -q Force printing of non-graphic characters in file names as the character `?';.
  • ls -Q Place double quotations around the entry names.
  • ls --help Detailed command here
  • ls -la /home you could check the contents of /home, regardless of your current directory.
View file

less /etc/services

less is an application that will continue to run and occupy the screen until you exit. scroll via ENTER OR UP DOWN arrow of keyboard.
Use /yourSeasrchQuery to search something . Press n for forward and N for see backword search result. Use q for exit.

Create a File with “touch”

touch file1
touch /home/sammy/file2 /home/sammy/file3 //create multiple file at same time even can use absoulate path

Create a Directory with “mkdir”

mkdir test/example // Test directory must exists
mkdir -p some/other/directories

you can use the -p option.This allows you to create nested directories in one step.The command will make the some directory first, then it will create the other directory inside of that. Finally it will create the directories directory within those two directories.

Moving and Renaming Files and Directories with “mv”

mv file1 test

Note: As with the mv command, it is possible to overwrite a file if you are not careful about the filename you are using as the target of the operation.

Copying Files and Directories with “cp”

cp file3 file4
cp -r some again // for directory

Removing Files and Directories with “rm” and “rmdir”

rm file1
rmdir some // For empty directory
rm -r some // for delete everything in some including some DIR

Show all environment variable

env

View the Value of a Variable

echo $PATH

You could use variable like below

cd $HOME

To set Env value

VAR=value

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment