Home Beginner's Guide 10 Linux commands for file and directory operations

10 Linux commands for file and directory operations

Navigating the Linux file system efficiently is crucial for any user. This guide introduces 10 essential commands that cover everything from listing directories with 'ls' to changing permissions with 'chmod'. Master these commands to confidently manage files and directories in Linux.

by Divya Kiran Kumar
linux commands for file and directory operations

Today, I’m excited to share my personal favorites and some indispensable Linux commands for file operations. These commands are the bread and butter of navigating and managing files in the Linux environment. Whether you’re just starting or looking to brush up on your skills, this guide is for you. Let’s dive in!

10 Linux commands for file and directory operations

1. ls – Listing files and directories

The ls command is your first window into the filesystem. It lists the contents of a directory, showing you all the files and subdirectories it contains. By default, it shows a simple list, but you can expand this view with various options. For instance, ls -l provides detailed information, including file permissions, the number of links, file owner, file size, and the last modification date. It’s a command I use almost every time I open the terminal – it’s that fundamental!

Input:

ls

Example Output:

Desktop  Documents  Downloads  Music  Pictures  Videos

You can also use ls -l for a detailed list, showing file permissions, number of links, owner, group, size, and last-modified date.

2. cd – Changing directories

The cd (change directory) command is your navigation tool in the Linux filesystem. It allows you to move from one directory to another. Without it, you’d be stuck in the same directory forever! It’s a command I use frequently to hop around the filesystem, especially when working on different projects. Remember, if you ever get lost, cd ~ will always take you back home.

Input:

cd Documents

Example Output: There’s usually no output, but you’ll be in the Documents directory now.

3. mkdir – Creating directories

mkdir, short for “make directory,” is your go-to command for organizing your files. It allows you to create new directories to store your files. I find it particularly useful when starting new projects or when I need to organize files into well-defined structures. It’s simple, but an absolute necessity for maintaining order in your filesystem.

Input:

mkdir NewFolder

Example Output: Again, no output, but a new directory named ‘NewFolder’ will be created.

4. rmdir – Removing directories

The counterpart to mkdir is rmdir, which removes empty directories. It’s a neat and safe way to clean up your workspace. However, it only works on empty directories. If you need to delete a directory containing files, you’ll have to use a more powerful command like rm -r. But be careful with it – there’s no going back once a directory is removed!

Input:

rmdir NewFolder

Example Output: No output, but ‘NewFolder’ will be removed if it’s empty.

5. touch – Creating files

The touch command is your file creation tool. It’s most commonly used to create a new, empty file quickly. I often use touch when I need to start a new script or text file. It’s also handy for updating file timestamps without modifying the file contents.

Input:

touch example.txt

Example Output: No output, but a new file ‘example.txt’ is created.

6. rm – Removing files

rm, short for “remove,” is the command for deleting files. It’s straightforward but powerful and should be used with caution. There’s no “undo” button in the Linux terminal, so once a file is deleted with rm, it’s usually gone for good. I always double-check which file I’m deleting before hitting enter.

Input:

rm example.txt

Example Output: No output, but ‘example.txt’ will be deleted.

7. cp – Copying files

The cp command is used for copying files from one location to another. It’s incredibly useful for creating backups, duplicating files, or moving data around without removing the original file. I often use it before making major changes to a file, just in case something goes wrong.

Input:

cp source.txt destination.txt

Example Output: No output, but ‘source.txt’ will be copied to ‘destination.txt’.

8. mv – Moving or renaming files

mv is a versatile command that can be used both to move files and directories from one location to another and to rename them. It’s like having two tools in one: a file mover and a renamer. I find it especially handy for organizing files and directories or quickly changing a file’s name.

Input for moving:

mv source.txt Documents/

Example Output: No output, but ‘source.txt’ is moved to the Documents directory.

Input for renaming:

mv source.txt newname.txt

Example Output: No output, but ‘source.txt’ is renamed to ‘newname.txt’.

9. cat – Displaying file content

The cat command, short for concatenate, is primarily used to display the contents of a file on the screen. It’s my go-to command for quickly viewing small files or checking a file’s contents without opening an editor. It can also be used to concatenate and display multiple files at once.

Input:

cat example.txt

Example Output:

This is the content of example.txt

10. chmod – Changing file permissions

Finally, chmod (change mode) is crucial for managing file permissions in Linux. It allows you to control who can read, write, or execute a file. I use chmod when I need to grant or restrict access to a file, especially for scripts that need to be executable.

Input:

chmod 755 example.txt

Example Output: No output, but the permissions of ‘example.txt’ are changed.

To verify that the permissions of ‘example.txt’ have been changed, you can use the ls -l command. This command lists files in the current directory with detailed information, including the file permissions. Here’s how you can do it:

Input:

 ls -l example.txt

Example Output:

-rw-r--r-- 1 user group size date time example.txt

In this output:

      • -rw-r--r-- indicates the file permissions.
      • 1 is the number of links to this file.
      • user is the owner of the file.
      • group represents the group ownership of the file.
      • size is the size of the file.
      • date and time show the last modification date and time.
      • example.txt is the name of the file.

By comparing the permissions shown in the output of ls -l with the changes you made using chmod, you can verify that the permissions of ‘example.txt’ have indeed been changed.

Linux file operations cheat sheet

This cheat sheet is a quick reference guide to some of the most frequently used commands for file operations in Linux. It’s designed to help you recall the basic functionalities of each command.

Command Description
ls Lists all files and directories in the current directory. Use ls -l for detailed information.
cd Changes the current directory. cd ~ takes you to the home directory.
mkdir Creates a new directory.
rmdir Removes an empty directory.
touch Creates a new, empty file or updates the timestamp of an existing file.
rm Removes files. Use with caution as there is no undo.
cp Copies files from one location to another.
mv Moves or renames files and directories.
cat Displays the content of a file or concatenates multiple files.
chmod Changes file permissions.

Frequently Asked Questions (FAQs) about Linux File Operations

Q1: What should I do if rm accidentally deletes an important file?
A1: Unfortunately, in most cases, once a file is deleted using the rm command, it’s permanently removed and not easily recoverable. This is why it’s crucial to use rm with caution. Some Linux systems have trash-cli, a command-line trashcan, which can provide a safety net, but it’s not a default feature.

Q2: How can I view hidden files using the ls command?
A2: To view hidden files (files that start with a dot), use ls -a. This command lists all files, including hidden ones. It’s useful for checking configuration files in your home directory.

Q3: Is there a way to undo a chmod command?
A3: There’s no direct “undo” command for chmod. However, if you remember the original permission settings, you can manually reset them using chmod again. It’s good practice to note down the original permissions if you’re unsure about the changes.

Q4: Can cp and mv work with directories?
A4: Yes, both can work with directories. Use cp -r to copy a directory and its contents, and simply mv to move a directory. Remember, mv will move all the contents of the directory as well.

Q5: How do I create a file with content using the terminal?
A5: You can use echo combined with a redirect operator. For example, echo "Hello, World!" > hello.txt creates a file named hello.txt with the content “Hello, World!”. Be careful with >, as it will overwrite existing content. Use >> to append instead.

Q6: What is the difference between rmdir and rm -r?
A6: rmdir only removes empty directories, making it a safer option. On the other hand, rm -r removes directories and their contents recursively, which is more powerful but also riskier if used improperly.

Q7: How can I rename a directory in Linux?
A7: You can use the mv command for renaming directories, just like with files. For example, mv oldDirName newDirName will rename the directory.

Q8: Is there a way to list files in reverse order?
A8: Yes, use ls -r to list files in reverse order. Combine it with other options like ls -lr for a detailed list in reverse order.

Q9: How can I copy a file to another directory and keep its original name?
A9: Simply specify the directory in the cp command. For example, cp file.txt /path/to/directory/ will copy file.txt into the specified directory, keeping the name file.txt.

Q10: How do I change the ownership of a file?
A10: Use the chown command. For example, sudo chown username:groupname file.txt changes both the user and group ownership of file.txt. You typically need superuser privileges to change file ownership.

Conclusion

Navigating the Linux file system is an indispensable skill in the repertoire of anyone working with this robust operating system. From the basic structure of the Linux file hierarchy to the utility of each command, we’ve covered the essentials that form the backbone of file system navigation and management.

These commands offer the foundation for a deeper understanding and more effective control over your Linux experience. With this knowledge, you’re well-equipped to explore the vast and intricate world of Linux with confidence and proficiency.

You may also like

Leave a Comment

fl_logo_v3_footer

ENHANCE YOUR LINUX EXPERIENCE.



FOSS Linux is a leading resource for Linux enthusiasts and professionals alike. With a focus on providing the best Linux tutorials, open-source apps, news, and reviews written by team of expert authors. FOSS Linux is the go-to source for all things Linux.

Whether you’re a beginner or an experienced user, FOSS Linux has something for everyone.

Follow Us

Subscribe

©2016-2023 FOSS LINUX

A PART OF VIBRANT LEAF MEDIA COMPANY.

ALL RIGHTS RESERVED.

“Linux” is the registered trademark by Linus Torvalds in the U.S. and other countries.