Create New Post

Working with files and directories

Working with files and directories is a fundamental aspect of managing a CentOS system. Here are some common tasks you might perform:

1. Navigating the File System:

  • Use the cd command to change directories. For example:
    cd /path/to/directory 
  • Use the pwd command to print the current working directory.

2. Listing Files and Directories:

  • Use the ls command to list files and directories in the current directory. For example:
    ls 
  • Use options like -l for a detailed list, -a to show hidden files, and -h for human-readable file sizes.

3. Creating Files and Directories:

  • Use the touch command to create an empty file. For example:
    touch filename 
  • Use the mkdir command to create a new directory. For example:
    mkdir directoryname 

4. Copying, Moving, and Renaming Files:

  • Use the cp command to copy files. For example:
    cp sourcefile destination 
  • Use the mv command to move or rename files. For example:
    mv oldname newname 

5. Removing Files and Directories:

  • Use the rm command to remove files. For example:
    rm filename 
  • Use the rmdir command to remove empty directories. For example:
    rmdir directoryname 
  • Use the rm -r command to remove directories and their contents recursively. Be cautious with this command, as it can delete files and directories permanently.

6. Viewing File Contents:

  • Use the cat command to display the contents of a file. For example:
    cat filename 
  • Use the less command to view long files one page at a time. For example:
    less filename 

7. Editing Files:

  • Use text editors like nano, vim, or emacs to edit files. For example:
    nano filename 
    This will open the file in the nano text editor, where you can make changes and save them.

8. Searching for Files:

  • Use the find command to search for files and directories based on various criteria. For example:
    find /path/to/search -name "filename" 
  • Use the grep command to search within files for specific patterns. For example:
    grep "pattern" filename 

9. Changing File Permissions and Ownership:

  • Use the chmod command to change file permissions. For example:
    chmod u+x filename 
  • Use the chown command to change file ownership. For example:
    chown username:groupname filename

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

22523