Quick Linux File Manipulation Commands Reference

Last Updated on

  • cd: Change directory
    • cd ~ or cd : Back to top folder
    • cd .. : Uppler level folder (../.. Uppler uppler)
  • pwd : Print working directory (Show current working directory)
  • ls : List files/folders within current folder
  • ls -l : With Detail
  • ls -a : Show all files, including hidden files (starts with dot “.”)
  • ls -lh : Show human readable size units
  • mkdir : Make directory
    • mkdir test : Create a folder named “test”
  • touch : Create file
    • touch mytext.txt : Create a text file named “mytext.file”
  • rm : Remove file, folder
    • rm -r : Remove folder and everything within that folder.
    • rm -f : Remove without confirming
    • rm -i : Confirm before removing
    • rm -rf : Remove the folder and everything within the folder without confirmation
    • rm -rf ./* : Remove everything within the current folder
  • mv : Move, Rename
    • mv file /folder1 : Move “file” under current folder to “folder1” withing current folder
    • mv filename1 filename2 : Rename from “filename1” to “filename2”
  • cp : Copy
    • cp /tmp/file ./ : Copy file under /tmp folder to current folder
    • cp -r myfolder/ folder2 : Copy everything from “myfolder” to “folder2”
  • find : Find/Search
    • find /tmp -name keyword : Search for “keyword” via name within “/tmp folder”
    • find /tmp -name ‘keyword*’ : Search for file names’ begin with “keyword” within “/tmp folder”
    • find /tmp -name ‘?keyword’ : Search for file names’ end with “keyword” within “/tmp folder”
    • find /tmp -size +3M : Search for files which is bigger than 3M
  • which : Show command path
    • which squid
  • cat : Show file content
    • cat /tmp/file : Show content within “/tmp/file”
  • more : Show content in multiple pages
    • more /etc/services
    • “Enter” key to show one more line
    • “Space” key to show one more page
    • “Q” key to exit
  • head : Show the head (top) of the file
    • head -n 10 /etc/services : Show first 10 lines of the “/etc/services” file
  • tail : Show the end of the file
    • tail -n 10 /etc/services : Show last 10 lines of “/etc/services” file
  • ln : Create link
    • ln -s SourceFile NewFile : Create soft link
    • ln SourceFile NewFile : Create hard link
''' Soft link/Symbolic link '''
# When the target file is removed, the soft link is useless
# Soft links can span file systems
touch
''' Soft link/Symbolic link '''
# When the target/source file is removed, the soft link is useless
# Soft links can span file systems
touch 1.txt
echo 'Some text' >> 1.txt #Write 'Some text' to 1.txt file
cat 1.txt #Output content from 1.txt
(Output) 123
ln -s 1.txt 2.txt
ls -l
(Output) 2.txt -> 1.txt #Editing 2.txt will actually edit 1.txt file, size of 2.txt won't change, only the target file 1.txt will change the size.
(Output) 1.txt
''' Hard link '''
# Delete any target/source file, the other one will still exist
# Hard links can't span file systems
touch 1.txt
echo 'Some text' >> 1.txt
ln 1.txt 2.txt
ls -ls
(Output) 1.txt
(Output) 2.txt
echo 'test 2' >> 1.txt
echo 'test 3' >> 2.txt
cat 1.txt
(Output) Some text
(Output) test 2
(Output) test 3
cat 2.txt
(Output) Some text
(Output) test 2
(Output) test 3
# Change the target/source or the hard link file will change the other one as well. Same size, with different name.

Leave a Reply

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