Linux Commands

👉 Present Working Directory(PWD)

To find out the current working directory, you can use the command…

pwd


If we want to see the actual physical path, we can use the -P flag.

pwd -p


👉 Open a folder or directory

open .


👉 Show hidden files and folder

Ctrl + H

And to hide the file and folder again, just press again.

Ctrl + H


👉 List Files and Directories

To see the list of folders and files in the current directory, use the following command…

ls

 

👉 Listing a Different Directory

To find out the files in another directory without leaving the current directory, use the following code…

ls /usr/local/


👉 Rename a file

mv <old file> <new file>


👉 Listing all files(-a)

To see both the “visible” and “hidden” files and directory, use the following code…

ls -a or ls -A


👉 Long-form listing(-l)

If we want to get more detailed information about the listing, then we can use the following code…

ls -l


👉 A long-form listing with human-readable sizes(-h)

If we want to get more detailed information about the listing with human-readable sizes, then we can use the following code…

ls -lh


👉 Long-form listing sorting by size(-s)

If we want to get more detailed information about the listing with human-readable sizes as well as sort of result by file size i.e; the large size files will show first , then we can use the following code…

ls -lhs


👉 Sorting by last modified time(-t)

If we want to sort out ls results by the last time files were modified...

ls -lt


👉 Reverse sort(-r)

Using the -r flag we are able to reverse the results of ls.

ls -lr


👉 Hard Links

If we want to create an identical copy from the existing file, then we can use the following code...

ln a.txt b.txt


N.B → a.txt is a source or existing file and b.txt is a created or target file.

Only work on the current file system.


👉 See the Content of a File

To the content of a file, use the following code…

cat a.txt


👉 Forcing a Link

If the source file already exists, then an error will show.

To fix the error, use the -f flag to force the link...

ln -f a.txt b.txt


👉 A symbolic link (-s)

Hard link (ln) does not work for the directory, for this we can use the -s flag which works for file as well as for the directory...

ln -s a.txt b.txt


👉 Change Directories(cd)

Change or navigate into another directory, use the command…

Here, the ~ will count from the root directory.

cd ~/Documents


👉 Navigating up (..)

Move up to the parent directory...

cd ..


👉 Navigating up (..)

Move up or change the directory from one child to another child directory in the same parent directory...

cd ../Documents


👉 Navigating to the home directory

Navigate back to the home directory, you can call cd without any arguments.

cd


👉 Creating Directories (mkdir)

To create a directory we can use the mkdir command followed by the name of the directory we wish to create.

mkdir foo


👉 Create Intermediate Directories(-p)

The mkdir command will allow us to create nested directories using the -p flag.

mkdir -p a/b/c     Notebook → a, b, and c are directories.


👉 Change or move to a directory or directories and create a file or files

cd test/read/mouse && touch index.html style.css script.js


👉 Create a file

The touch command will allow us to create a file or files.

touch test.txt or touch index.html style.css


👉 Verbose output (-v)

Adding the -v flag will print the results of mkdir to the console.

mkdir -v a


👉 Copying Single File (cp)

By using the cp command we can copy single and directory.

cp a.txt b.txt


N.B → Here a.txt is the source file and b.txt is the target file.


👉 Copying Multiple Files (cp)

By using the cp command we can copy multiple files and directories but the last argument must be a directory.

cp a.txt b.txt foo


N.B → Here foo is a directory.


👉 Copying Directories (-R)

If we want to copy a directory to another directory, we can use -R flag with cp.

cp -Rv foo bar


👉 Force Overwriting a file

If we want to copy a file from a user to a different user, then we have to use the command...

cp -f a.txt b.txt foo


👉 Confirm overwriting a file(-i)

If we want to show a confirmed message which files will be overwritten, then we use -i flag with cp. 

cp -i a.txt b.txt foo


N.B → then a message will show like overwrite b.txt? (y/n [n])

Now you have to type y and press enter.



👉 Remove or Delete an empty Folder or Directory

rmdir directory name


👉 Remove or Delete nested Folders or Directories

rmdir -p test/html/rest


👉 Remove all files from a directory

rm media/*


👉 Remove or Delete a Folder or Directory with sub directories

rm -r directory name


👉 If you get ‘permission denied’ or any error then use the following code to remove or delete

sudo rm -r directory name


👉 Remove or Delete entire or full Folders or Directories(with force)

rm -rf directory name

// N.B → don’t always use this code.


👉 Create a directory and move to this directory

mkdir directory_name && $_ → press enter


👉 Remove or Delete a File

To remove a file, use the following code …

rm a.txt

Or

rm -v a.txt

N.B → rm will work the same as cp.


👉 Moving Files (-mv)

The process for moving files is almost identical to that of copying files. In reality, the mv command is really just a combination of cp and rm.


mv -v a.txt b.txt

mv  [filename]  [dest-dir]

mv  wonderwoman.txt  batman.txt  superhero/

mv fullnames.txt  /home/himanshu/Downloads



👉 Input / Output (|, >)


đź’¨ Redirecting output(|):

We can redirect the output of one command to the input of another command. This is made possible by using the “pipe” operator, |.


In the following example, we “pipe” the output of the ls command to the input of the grep command to find all the files in my home directory that contain an underscore, _.

ls -a ~ | grep _


By using the pipe | operator we can chain together any number of commands. Here, the output or listing of grep command passes to the sed command and changes all of the underscores to dashes.

 $ ls -a ~ | grep _ | sed "s/_/-/g"


đź’¨ Wildcards

cp * satire/

*  These types of special characters are called wildcards. 

 The * selects all files in the working directory, so here we use cp to copy all files into the satire/ directory.


Here, 

cp m*.txt scifi/

Here, m*.txt selects all files in the working directory starting with “m” and ending with “.txt”, and copies them to scifi/.

đź’¨ stdin, stdout, and stderr:

echo "Hello" > hello.txt

cat hello.txt

cat oceans.txt > continents.txt


đź’¨ >>:

cat glaciers.txt >> rivers.txt

>> takes the standard output of the command on the left and appends (adds) it to the file on the right.


đź’¨ <:

cat < lakes.txt


đź’¨ |:

cat volcanoes.txt | wc 

cat volcanoes.txt | wc | cat > islands.txt


đź’¨ Sort:

sort lakes.txt

cat lakes.txt | sort > sorted-lakes.txt


đź’¨ uniq:

uniq deserts.txt


đź’¨ sort deserts.txt | uniq

sort deserts.txt | uniq > uniq-deserts.txt


đź’¨ grep:

grep Mount mountains.txt

grep -i Mount mountains.txt

grep -R Arctic /home/ccuser/workspace/geography

grep -Rl Arctic /home/ccuser/workspace/geography

grep -R player .


đź’¨ sed:

sed 's/snow/rain/' forests.txt

$ sed 's/snow/rain/g' forests.txt


Post a Comment

Previous Post Next Post