Skip to content

Instantly share code, notes, and snippets.

@Sandip124
Created November 25, 2021 12:54
Show Gist options
  • Save Sandip124/d4996a6e4ccde6165408394a27da590c to your computer and use it in GitHub Desktop.
Save Sandip124/d4996a6e4ccde6165408394a27da590c to your computer and use it in GitHub Desktop.
Learning Linux

Learning Linux

aptitude moo
ls -l

listing with detail

ls -l -r

-> reverse the listing

r is used to reverse the lising

-v verbose

ls -v
aptitude -v moo

try adding multiple v and see the different response message

for example:

aptitude -vv moo"

and try this

aptitude -vvvvv moo"

PWD (Print Working Directory)

In order to discover where you are currently located within the filesystem, the pwd command can be used. The pwd command prints the working directory, your current location within the filesystem:

pwd

systemadmin@localhost:~$

the phrase in the terminal (~) in blue color indicates /home/sysadmin representing home directory

Chaning directory

If you are already using the terminal on linux. You wonder that you use the cd command to navigate when going from one folder to another folder in linux or bash. the the cd command indicates the word Change directory

cd .. 

to go one directory back in the terminal

 cd ~ 

to go to the home directoy

i.e. home/sysadmin

Listing Files

 ls
 ls -l /var/log/
Symbol File Type Description
d directory A file used to store other files.
- regular file Includes readable files, images files, binary files, and compressed files.
l symbolic link Points to another file.
s socket Allows for communication between processes.
p pipe Allows for communication between processes.
b block file Used to communicate with hardware.
c character file Used to communicate with hardware.
drwxr-x--- 2 root   adm    4096 Dec 20  2017 apache2 
  1. Permissions
  2. Hard Link Count
  3. User Owner
  4. Group Owner
  5. File Size
  6. Timestamp
  7. FileName

Administrative Access

The su command

sl

After executing the sl command you would probably see the train running from left to right in your command line.

Permission

-rw-r--r-- 1 sysadmin sysadmin 647 Dec 20  2017 hello.sh 
Permission Effects on File Effects on Directory
read (r) Allows for file contents to be read or copied. Without execute permission on the directory, allows for a non-detailed listing of files. With execute permission, ls -l can provide a detailed listing.
write (w) Allows for contents to be modified or overwritten. Allows for files to be added or removed from a directory. For this permission to work, the directory must also have execute permission.
execute (x) Allows for a file to be run as a process, although script files require read permission, as well. Allows a user to change to the directory if parent directories have execute permission as well.

Changing file permissions

chmod [<SET><ACTION><PERMISSIONS>]... FILE
-rw-r--r-- 1 sysadmin sysadmin 647 Dec 20  2017 hello.sh   

Chaging File Ownership

sudo chown root hello.sh

Viewing Files

cat filename.txt
  • Show first five lines of the contentx of alpha.txt
head -n 5 alpha.txt
  • Show last five lines of content of alpha.txt
tail -n 5 alpha.txt 

Copying files in linux

Copy passwd to current directory

The (.) represents the current directory as we already discuss above.

cp /etc/passwd .

Copying files

The dd command is a utility for copying files or entire partitions at the bit level.

This command has several useful features, including:

  • It can be used to clone or delete (wipe) entire disks or partitions.
  • It can be used to copy raw data to removable devices, such as USB drives and CDROMs.
  • It can backup and restore the MBR (Master Boot Record).
  • It can be used to create a file of a specific size that is filled with binary zeros, which can then be used as a swap file (virtual memory).
dd if=/dev/zero of=/tmp/swapex bs=1M count=50 

Moving Files

The mv command is used to move a file from one location in the filesystem to another.

mv SOURCE DESTINATION

The mv command requires at least two arguments. The first argument is the source, a path to the file to be moved. The second argument is the destination, a path to where the file will be moved to. The files to be moved are sometimes referred to as the source, and the place where the files are to be placed is called the destination.

Move people.csv to work directory.

mv people.csv Work 

using mv we can move multiple files to another directory

mv numbers.txt letters.txt alpha.txt School  

Removing Files

The rm command is used to delete files and directories. It is important to keep in mind that deleted files and directories do not go into a "trash can" as with desktop-oriented operating systems. When a file is deleted with the rm command, it is almost always permanently gone.

rm linux.txt

The rm command will ignore directories that it's asked to remove; to delete a directory, use a recursive option, either the -r or -R options. Just be careful since these options are "recursive", this will delete all files and all subdirectories:

rm -r Work

Filtering Input

The grep command is a text filter that will search input and return lines which contain a match to a given pattern.

grep [OPTIONS] PATTERN [FILE]

Use sysadmin as the pattern argument and passwd as the file argument:

grep sysadmin passwd 

Regular Expressions

Regular expressions have two common forms: basic and extended. Most commands that use regular expressions can interpret basic regular expressions. However, extended regular expressions are not available for all commands and a command option is typically required for them to work correctly.

Basic Regex Character(s) Meaning
. Any one single character
[ ] Any one specified character
[^ ] Not the one specified character
  • | Zero or more of the previous character ^ | If first character in the pattern, then pattern must be at beginning of the line to match, otherwise just a literal ^ $ | If last character in the pattern, then pattern must be at the end of the line to match, otherwise just a literal $
Extended Regex Character(s) Meaning
  • | One or more of the previous pattern ? |The preceding pattern is optional { } |Specify minimum, maximum or exact matches of the previous pattern | | Alternation - a logical "or" ( ) |Used to create groups

Basic Patterns

Regular expressions are patterns that only certain commands are able to interpret. Regular expressions can be expanded to match certain sequences of characters in text. The examples displayed on this page will make use of regular expressions to demonstrate their power when used with the grep command. In addition, these examples provide a very visual demonstration of how regular expressions work, the text that matches will be displayed in a red color.

 grep sysadmin passwd                               

Anchor Characters

Anchor characters are one of the ways regular expressions can be used to narrow down search results. For example, the pattern root appears many times in the /etc/passwd file:

 grep 'root' passwd

The first anchor character ^ is used to ensure that a pattern appears at the beginning of the line. For example, to find all lines in /etc/passwd that start with root use the pattern ^root. Note that ^ must be the first character in the pattern to be effective.

grep '^root' /etc/passwd

The second anchor character $ can be used to ensure a pattern appears at the end of the line, thereby effectively reducing the search results. To find the lines that end with an r in the alpha-first.txt file, use the pattern r$:

grep 'r$' alpha-first.txt

One of the most useful expressions is the period . character. It will match any character except for the new line character. The pattern r..f would find any line that contained the letter r followed by exactly two characters (which can be any character except a newline) and then the letter f:

grep 'r..f' red.txt

matched values roof reef

grep '[0-9]' profile.txt


grep '[.]' profile.txt


grep 're*d' red.txt

grep 'r[oe]*d' red.txt


grep 'e*' red.txt

Shutting Down

The shutdown command arranges for the system to be brought down in a safe way. All logged-in users are notified that the system is going down and within the last five minutes leading up to the shutdown, new logins are prevented.

shutdown [OPTIONS] TIME [MESSAGE]

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