DEV Community

Emdadul Islam
Emdadul Islam

Posted on

Title: A Beginner's Guide to Command-Line File and Directory Manipulation

In the realm of command-line interfaces, understanding basic commands for file and directory manipulation is a fundamental skill. Whether you're navigating through your file system or creating, modifying, and deleting files and directories, these commands empower users to efficiently manage their digital workspace. In this guide, we'll explore essential command-line commands and create a cheat sheet for quick reference.

Navigation Commands:

  1. pwd (Print Working Directory):
    • Displays the full path of the current working directory.
   pwd
Enter fullscreen mode Exit fullscreen mode
  1. ls (List):
    • Lists the contents of the current directory.
   ls
Enter fullscreen mode Exit fullscreen mode
  1. cd (Change Directory):
    • Change into an existing directory.
   cd directory-name
Enter fullscreen mode Exit fullscreen mode
  1. cd .. (Move Backward):
    • Move back one directory level (to the parent directory).
   cd ..
Enter fullscreen mode Exit fullscreen mode
  1. cd directory/sub-directory:
    • Change forward through multiple directories.
   cd directory/sub-directory
Enter fullscreen mode Exit fullscreen mode
  1. cd ../.. (Move Backward Multiple):
    • Move backward through multiple directories.
   cd ../..
Enter fullscreen mode Exit fullscreen mode

File and Directory Manipulation:

  1. mkdir (Make Directory):
    • Create a new directory.
   mkdir directory-name
Enter fullscreen mode Exit fullscreen mode
  1. touch:
    • Create a new file.
   touch file-name
Enter fullscreen mode Exit fullscreen mode
  1. echo "text" > file:
    • Overwrite a file with specified text.
   echo "text" > file
Enter fullscreen mode Exit fullscreen mode
  1. echo "text" >> file:

    • Append text to the end of a file.
    echo "text" >> file
    
  2. cat file-a > file-b:

    • Overwrite content of file-b with content of file-a.
    cat file-a > file-b
    
  3. cat file-a >> file-b:

    • Append content of file-a to content of file-b.
    cat file-a >> file-b
    
  4. mv source-file target-file:

    • Move/rename source-file to target-file.
    mv source-file target-file
    
  5. mv source-dir target-dir:

    • Move/rename source-directory to target-directory.
    mv source-dir target-dir
    
  6. rm file:

    • Delete a file.
    rm file
    
  7. rm -r directory:

    • Delete a non-empty directory.
    rm -r directory
    
  8. rmdir directory:

    • Delete an empty directory.
    rmdir directory
    
  9. cp file-a > file-b:

    • Copy content of file-a into file-b (overwrite).
    cp file-a > file-b
    
  10. cp -r dir-a dir-b:

    • Copy content of dir-a into dir-b (overwrite).
    cp -r dir-a dir-b
    

Additional Commands:

  1. clear:

    • Clear the command line.
    clear
    

Conclusion:

Mastering these basic command-line operations provides a solid foundation for navigating and managing files and directories in a terminal environment. Always exercise caution, double-check your commands, and remember that certain actions, such as deletion, can be irreversible. With this cheat sheet, you're well on your way to becoming proficient in command-line file and directory manipulation. Happy coding!

Top comments (1)

Collapse
 
moopet profile image
Ben Sinclair • Edited

I think your numbering got eaten by the editor! Almost all the examples are number 1.

There's a syntax error in your example 9; it should be cp file-a file-b.

In your 8th example 1 , touch isn't a command to create a file - that's a side effect of its real purpose, and there are lots of commands which will incidentally create a file for you. More importantly (or pedantically?) you don't need to do it so it's something beginners don't need to learn.