In Linux system, a file doesn’t include only text files, images and compiled programs, partitions, hardware device drivers and directories. Linux consider everything as as file are also files.
Create a text file
touch test.txt // using vi vi tes.txt // using vim vim test.txt
Update content of an existing file
// using vi vi tes.txt // using vim vim test.txt
Delete a file with rm command
rm means remove. This command is used to remove a file. The command line doesn’t have a recycle bin or trash unlike other GUI’s to recover the files. Hence, be very much careful while using this command. Once you have deleted a file, it is removed permanently.
rm test.txt // delete multiple files rm test1.txt test2.txt // delete files having same extension. rm *.json // delete a directory recursively rm -r test // delete files/directories recursively and forcefully rm -rf
Copy a file with cp command
cp stands for copy.
folaukaveinga@Folaus-MacBook-Pro-3 files % ls test.txt folaukaveinga@Folaus-MacBook-Pro-3 files % cp test.txt test1.txt folaukaveinga@Folaus-MacBook-Pro-3 files % ls test.txt test1.txt
Copy multiple files
// copy all directories and files int the current directory to the Download directory copy -R * /Users/folaukaveinga/Download
Move a file from one place to another with mv command
mv command is used to move existing file or directory from one location to another. It is also used to rename a file or directory. If you want to rename a single directory or file then mv option will be better to use.
mv file destination
folaukaveinga@Folaus-MacBook-Pro-3 files % ls test test.txt test1.txt folaukaveinga@Folaus-MacBook-Pro-3 files % mv *.txt test folaukaveinga@Folaus-MacBook-Pro-3 files % ls -la total 0 drwxr-xr-x 3 folaukaveinga staff 96 Nov 7 22:47 . drwx------@ 8 folaukaveinga staff 256 Nov 7 20:42 .. drwxr-xr-x 4 folaukaveinga staff 128 Nov 7 22:47 test folaukaveinga@Folaus-MacBook-Pro-3 files % cd test folaukaveinga@Folaus-MacBook-Pro-3 test % ls test.txt test1.txt folaukaveinga@Folaus-MacBook-Pro-3 test %
Rename a file
To rename a file there are other commands also like ‘mv’. But ‘rename’ command is slightly advanced then others. This command will be rarely used and it works differently on different distros of linux.
Get file type
file command is used to determine the file type. It does not care about the extension used for file. It simply uses file command and tell us the file type.
folaukaveinga@Folaus-MacBook-Pro-3 files % file test.txt test.txt: ASCII text