Linux/Mac Command Line Cheat Sheets

command-line-cheat-sheet

Navigating the file system:

 $ pwd : Prints the current working directory.

$ cd :    Changes the current directory.

$ cd my_directory : To change current directory to a directory called ‘my directory’. That directory must exist in the current directory.

$ cd my_dir1/my_dir2/mydir3 : To change the current directory to a subdirectory called ‘mydir3’. ‘mydir3’ is subdirectory under multiple levels.        

$ cd/.. : To change the current director back out one level.

$ cd/../../.. : To change the current director back out Three level.

  $ls :     Lists the files and directories in the current directory.

$ ls : To see items in the current directory. Only shows the names of the items.

$ ls -la  : To list the files and directories in more detail. You can tell if an item is a file or a directory. It will also show the owner and size of the items.

 Creating and managing files and directories:

$ mkdir : Creates a new directory.

$ mkdir directoryName

$ touch : Creates a new file.

$ touch myFile1

$ mv :   Moves or renames a file or directory.

$ mv file  ” new file path” : To move files to new location

$ mv filename  new_file_name : To rename the files to a new filename

$ cp :  Copies a file.

$ cp filename ”new directory path”

$ cp -r : Copies a directory. (recursive)

$ cp -r name “path”

$ rm : Deletes a file.

         $ rm filename

$ rm -r : Deletes a directory.

Viewing and editing files:

$ less :   Allows you to view the contents of a file one page at a time.

$ less filename

$ nano :  A simple text editor to edit the file in terminal

$ nano new_filename

$ vi  / $vim :   A more advanced text editor to edit the files in terminal

$ vim filename.txt

$ cat :  Displays the contents of a file.

$ cat filename

Searching and filtering:

$ grep :  Searches for a specific string in a file or multiple files.

$ grep [options] pattern [files]

the -i option enables Case insensitive search

                $grep -i ‘Word’  filename

$ find :  Search for files in a directory hierarchy.

$ find [where to start searching from] [expressions determine what to find] [options] [what to find]

$ find  ./GFG -name filename : To search a file with a specific name

               

Permissions and ownership:

$ chmod :  Changes the permissions of a file or directory.

$ chomd u = rw new_filename : To give a read and write permission

                  $ chown : Changes the ownership of a file or directory.

                 $ chown [option] [owner] [:[Group]] file : To change owner of a file

                 $ chown owner_name file_aname

Miscellaneous:

$ clear : Clears the terminal screen.

$ exit : Closes the terminal window.

$ man : Displays the manual pages for a command.

Learn the complete Python programming language

Read more

Stay connected with news and updates!

Join our mailing list to receive the latest news and updates from our team.
Don't worry, your information will not be shared.

We hate SPAM. We will never sell your information, for any reason.