Menu

Monday, July 4, 2011

File related commands

  • 1. How to create an empty file?
    • Syntax
    • # touch file-name
    • Example:
    • # touch tmp
    • The above command creates an empty file.
  • 2. How to copy files and directories?
    • Syntax
    • # cp file1 file2
    • Example:
    • # cp names.txt emp.txt
    • The above command copies content of names.txt to emp.txt if you agree for overwrite.
    • # cp -r /opt/dev/project1 /opt/dev/project2
    • The above command copies all the contents of project1 directory to project2 directory recursively.
  • 3. How to read a file?
    • Syntax
    • # more file-name
    • Example:
    • # more /etc/passwd
    • The above command reads passwd file
  • 4. How to concatenate files?
    • Syntax
    • # cat file1 file2
    • Example:
    • # cat /tmp/user-list1.txt /tmp/user-list2.txt > users.txt
    • The above command concatenates both users list file and writes into a "users.txt" file.
  • 5. How to create a directory?
    • Syntax
    • # mkdir directory-name
    • Example:
    • # mkdir downloads
    • The above command creates directory named as "downloads"
    • # mkdir –p /usr/softwares/downloads
    • The above command creates a directory with parent as needed if parent doesn't exist.
  • 6. How to change the current directory?
    • Syntax
    • # cd
    • Example:
    • # cd /opt/dev/project
    • The above command change the current directory to /opt/dev/project
    • # cd ../
    • Go one level up from the current folder
  • 7. How to get current directory path?
    • Syntax
    • # pwd
    • The above command prints the current directory path.
  • 8. How to rename a file?
    • Syntax
    • # mv file1 file2
    • Example:
    • mv names-list.txt emp-list.txt
    • The above command moves the content of "names-list.txt" to emp-list.txt. As Linux doesn't support rename, move command is used.