Linux Commands
cd
cd: directs to/homecd ..: Goes to parent of current directorycd /folder: using absolute paths,folderstart from the root folder/cd /mnt: Mount Drivescd folder: Goes to folderfolderin this directoryexplorer.exe .: opens in windows file explorer (WSL)
ls
ls: Lists all folders in this directoryls -al: Gives more informationYou have, from left to right:
- the file permissions (and if your system supports ACLs, you get an ACL flag as well)
- the number of links to that file
- the owner of the file
- the group of the file
- the file size in bytes
- the file modified datetime
- the file name
Note
- This set of data is also generated by the
loption. - The
aoption instead also shows the hidden files. Hidden files are files that start with a dot (.) - For file permission : r = read , w = write , x = executions access
mkdir
mkdir folder: creates folder namedabcmkdir folder1 folder2: creates multiple folders at oncemkdir -p folder/nested_folder: creates multiple nested folders by adding the-poption
rm | rmdir
rm file: deletes filermdir folder: deletes folder (The folder you delete must be empty.)rm -rf folder: deletes files and folders and everything included
touch
touch abc: creates an empty file (If the file already exists, silently fails)
mv
mv abc new_abc: if there is no folder ,abcwill be renamed tonew_abc. Otherwiseabcwill be moved to that foldermv a1 a2 abc: movesa1,a2files to folderabc
pwd
pwd: prints current path
chmod
chmod: changes read write execute permissions (Details)
cat
cat in1orcat < in1: Reads filecat > in1: Writes to file and thenctrl+cto exit editingcat >> in1: Appends to file and thenctrl+cto exit editingcat -n in1: prints content ofin1with line numberscat in1 in2: print contents of multiple filescat < in1 > in2concatenate contents of single file into new filecat in1 in2 > in3: concatenate contents of multiple files into new filecat in3 | anothercommand: add another command using|
head | tail
head -n file_name: prints first n linestail -n file_name: prints last n lines
wc
word count
wc -l file1: count lineswc -w file1: count wordswc -c file1: count characterswc -m file1: count characters with multibyte support (i.e. emojis count as 1, not as multiple characters)wc -m in*: operate wc -m on all files starting within
find
find . -name '*.txt': Finds all files with.txtextensionfind . -name 'in*': Finds all files and folders starting with infind . -type d -name "in": search only directoryfind . -type f -name "in": search only filesfind . -type l -name "in": search only linksfind . -type f -name 'in*' -mtime +3: Search files edited more than 3 days agofind . -type f -name 'in*' -mtime -1: Search files edited in the last 24 hoursfind . -type f -mtime -1 -delete: delete those who meet the criteriasfind . -type f -size +100c: search files that have more than 100 characters (bytes) in themfind . -type f -size +100k -size -1M: search file size bigger than 100KB but smaller than 1MB
find . -type f -exec cat {} \;execute a command on each result of the search . {} is filled with the file name at execution time.