The following Linux commands are those I’ve come across that are used to manipulate files, and the notes I’ve made to help me remember what they do.
cp – copy
cp copies a file or directory from the source to the destination and leaves the original file intact cp Documents/n* ~ #This will copy the all files beginning with lower case n in the Documents directory of the current working directory into the users HOME directory (~) #directories cannot be copied this way and may cause errors. To view errors, use verbose mode eg: cp -v Documents/n* ~ #this will display alerts as the copy operation runs. #to copy directories use recursive options -r or -R cp -R -v Documents/n* ~
file determines what content is contained by a file. This might be useful to prevent opening a file to read that cannot be handled by the terminal. file Documents/noobot.txt Displays: Documents/noobot.txt: ASCII text indicating that this is indeed a text file. file /bin/cat might display: /bin/cat: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), dynamically lin ked, interpreter /lib/ld-linux-armhf.so.3, for GNU/Linux 3.2.0, BuildID[sha1] =2239a192f2f277bd1a4892e39a41eba97266b91f, stripped This is an Executable Link Format file which would not display well in the terminal if using a command like cat on it.
ls – list
ls is used to list files/directories -a #displays all files including hidden -A #as -a option but omits . and .. (current/parent directories) -d #displays directory information only not files -l #long listing -r #reverses the order of a file sort -R #lists recursively (ie displays contents of sub directories) -S #sorts files by size -t #sorts files by modified time Long listing format: drwxr-xr-x 1 mrnoobot admin 2048 May 29 2020 noobdocs d #denotes file type (see below - this is a directory) rwxr-xr-x #file permissions - user/group/other - read/write/execute 1 #hard link count (only one directory links to this file) mrnoobot #this field is the user owner admin #this field is the group ownership 2048 #this field is the file size May 29 2020 #last modified timestamp noobdocs #file/directory name File types: d #directory - #regular file l #symbolic link s #socket p #pipe b #block file c #character file
mkdir – make directory
mkdir creates DIRECTORIES mkdir noobdocs #creates the directory noobdocs mkdir noobdocs noobdocs1 noobdocs2 #creates multiple directories in the current working directory mkdir -p noobdocs/noobdocs1/noobdocs2 #creates directories in a parent/child structure; eg noobdocs will be created in the current working directory; noobdocs1 will be created in noobdocs; noobdocs2 will be created in noobdocs1
mv – move
the mv command is similar to the cp command except it DOES NOT leave the source file intact; it physically moves it like a CUT/PASTE operation. The final argument will typically be a destination DIRECTORY. However, if the final argument is a FILENAME, then the file will be RENAMED. mv noobfile changefile.txt #renames the file noobfile to changefile.txt mv noobfile Documents/noobdocs #move a single file into directory noobdocs mv noobfile noobfile1 noobfile2 Documents/noobdocs #move multiple files into the directory noobdocs
rm – remove
rm can be used to remove FILES and recursively to remove DIRECTORIES AND THEIR CONTENTS rm noobfile.txt rm -i n*.txt #adding i option prompts user for each file - without it the above command would delete all .txt files that start with lower case n. rm -r noobdocs #deletes the directory noobdocs AND EVERYTHING IN IT!
rmdir – remove directory
rmdir will delete EMPTY directories only. If trying to delete directories and contents, use rm recursively instead. rmdir noobdocs #deletes the EMPTY directory noobdocs. If it has any content an error will occur rmdir -p noobdocs/noobdocs1/noobdocs2 #deletes the parent/child directory structure noobdocs/noobdocs1/noobdocs2 provided ALL directories are empty
touch has a variety of uses including creating empty files and updating timestamps. touch noobfile #if noobfile does not exist, it will be created as an empty file touch noobfile #since it exists, the modified timestamp will be updated -a #updates the accessed timestamp -c #updates the file attribute modification timestamp (metadata)