Terminal File Operations Commands

Terminal is an application that runs commands given by user, actually there is a special program inside Terminal called Shell – which reads in commands and execute them on Mac OS operating system.Terminal’s only job is to open windows and manage shells, while shells actually do real work of Command Execution. You can enter commands in Terminal and then Shell Program running inside terminal will execute those commands.

Commands are a kind of way to interact with Operating System and File System. These can be used for creating files/directories or checking status of processes being run by the Operating System. That’s why these can be really useful, moreover terminal commands can be used for building programming projects as well. Like you can build a Django Framework based website just from Terminal. Moreover, Terminal can be used for compiling/executing multiple files in a large Software Engineering Project. That’s why every beginner in Software Engineering should be aware about What is Terminal, How does it works and what are commands which can be used in it? Also if in case you already don’t know what is terminal then check out this article – What is Terminal in MAC OS?.
Anyway let’s get into what File Operations commands can be used in Mac Terminal.

File Operations Commands

These are commands which can be used for dealing with Files in Mac.

Below is a Table containing Terminal File Operations Commands

File Operation CommandDescription
lsLists all the files in a Directory
cpFor copying a file
mvFor moving a file from one location to another
rmFor removing a file
lnFor creating links to a File

Let’s discuss each one of five File Operation Commands one-by-one.

ls – File Operation Command

ls command is used for listing files or directories in a directory.
ls command in general can be written as ls [options] [files] where options can be -a, -l etc.

Using ls file operation command in Mac Terminal

Options for ls – File Operations Command

ls Command OptionsDescription
ls -aLists all files(including those files whose name begins with dot)
ls -lLists all files in a long way, including file attributes as well
ls -@Display OS X extended file attributes files
ls -FLists files, but decorates their names so as to make clearly shows type of file
• Appends “/” to directories
• Appends “*” to executables
• Appends “@” to symbolic links
• Appends “|” to names pipes
• Appends “=” to sockets
(Do not that while using -F options with ls command, whatever symbols are printed out are actually not part of filename rather these are virtual just to show user different file types)
ls -iPrepend inode numbers of files
ls -sPrepend size of file in blocks
ls -RLists all of directory content recursively. For example – If a directory have another directory then another directory. Then lists all of directories content until there is not further directory inside a directory.

ls -a File Operations Command

Lists all files(including those files whose name begins with dot).

ls -a File Operation Command

ls -l File Operation Command

Lists all files in a long way, including file attributes as well.

ls -l File Operation Command

ls -@ File Operation Command

Display OS X extended file attributes files.

ls -@ File Operation Command

ls -F File Operation Command

Lists files, but decorates their names so as to make clearly shows type of file
• Appends “/” to directories
• Appends “*” to executables
• Appends “@” to symbolic links
• Appends “|” to names pipes
• Appends “=” to sockets
(Do not that while using -F options with ls command, whatever symbols are printed out are actually not part of filename rather these are virtual just to show user different file types).

ls -F File Operation Command

ls -i File Operation Command

Prepend inode numbers of files and then list the files, here inode number means an unique identification number of a file/directory in a file system. For example – In the below picture inode number of directory Applications is 7161643 and similarly other files/directories have unique inode number.

ls -i File Operation Command

ls -s File Operation Command

Prepend size of file (Blocks Size).

ls -s File Operation Command

ls -R File Operation Command

Lists all of directory content recursively. For example – If a directory have another directory then another directory. Then lists all of directories content until there is not further directory inside a directory.
Below is a video showing Recursively listing of files in a directory.

cp – File Operation Command

cp command is used for copying files from one location to another in the filesystem.
cp command in general can be written as cp [options] file1 file2 or cp [options] (files | directories) directory.

Options for cp – File Operations Command

cp Command OptionsDescription
cp -pCopies file from one location to another and also copies file permissions, timestamps, file owner details.
(Kind of copying meta data of file alongwith file’s content)
cp -aCopy a directory hierarchy recursively, preserving all file attributes and links.
cp -RCopies a directory hierarchy recusively, but doesn’t copies file’s attributes(File Permission, Time Stamp) rather it does copies symbolic links.
cp -iInteractive mode of copying the file, it asks user whether to overwrite a file at destination location or not before copying the files over to destination location
cp -fThis forces the copying of files to destination location. For example – At destination location if file already exists, then it will just overwrite that file.

cp -p File Operation Command

Copies file from one location to another and also copies file permissions, timestamps, file owner details.
(Kind of copying meta data of file alongwith file’s content)
In the example shown in picture below – Folder1 have a file f.txt and Folder2 is empty. Using cp -p command, f.txt have been copied from Folder1 to Folder2.

cp -p File Operation Command

After copying file from one folder to another, by going to ‘Get Info’ section by right clicking on f.txt in Folder1 and Folder2 you can clearly see that these two files have same Created/Modified dates and File Accessing Permissions as well.

cp -a File Operation Command

Copy a directory hierarchy recursively, preserving all file attributes and links.

cp -R File Operation Command

Copies a directory hierarchy recusively, but doesn’t copies file’s attributes(File Permission, Time Stamp) rather it does copies symbolic links.

cp -i File Operation Command

Interactive mode of copying the file, it asks user whether to overwrite a file at destination location or not before copying the files over to destination location.
So if you have two folders – Folder1 and Folder2 both having file f.txt, and your copying f.txt from Folder1 to Folder2. Then terminal will show a little message saying your going to overwrite the file. Click y or n based upon whether you want to overwrite or not.

cp -f File Operation Command

This forces the copying of files to destination location. For example – At destination location if file already exists, then it will just overwrite that file. This is kind of a version of cp -i command, but not shows any pop up before overwriting the file.

mv – File Operation Command

mv command is used for moving files from one location to another.
mv command in general can be written as mv [options] source target.

Options for mc – File Operations Command

mv Command OptionsDescription
mv -iInteractive asks user if to write a file at target location
mv -fDon’t ask user for overwriting permission, just overwrite file at target location

mv -i File Operation Command

Interactive asks user if to write a file at target location. In the example shown in picture below, both folders Folder1 and Folder2 have a file f.txt, when using command command mv -i file f.txt is being moved from Folder1 to Folder2 then a little overwriting pop up will show up to ask user whether to procede overwriting or not.

mv -i command

mv -f File Operation Command

Don’t ask user for overwriting permission, just overwrite file at target location. In the example shown in picture below, both folders Folder1 and Folder2 have a file f.txt, when using command mv -f file f.txt is being moved from Folder1 to Folder2 then nothing shows up to show user that there is already a f.txt file in Folder2 rather command mv -f just procede with task and overwrite f.txt file in Folder2.

mv -i command

rm – File Operation Command

rm command is used for removing files.
rm command in general can be written as rm [options] files | directories.

Options for rm – File Operations Command

rm Command OptionsDescription
rm -iAsk user a yes/no question before actually removing file
rm -fForce delete a file and ignore all errors/warnings
rm -rRecursively remove a directory and it’s content

rm -i File Operation Command

Ask user a yes/no question before actually removing file. In the picture shown below, there is a f.txt inside Folder1. Using command rm -i f.txt firstly Terminal will ask remove f.txt? If user enter y and press enter then f.txt would be removed otherwise not.

rm -i Command

rm -f File Operation Command

Force delete a file and ignore all errors/warnings. This command rm -f also removes file, but in an unsafe way. This command doesn’t ask user a yes/no question before actually deleting file like rm -i command mentioned just above.

rm -f Command

rm -r File Operation Command

Recursively remove a directory and it’s content. This command rm -r will remove all of files inside a directory.

ln – File Operation Command

ln command is used for making links to files in a FileSystem or in general for linking files together. There can be of two types of File Links – Symbolic Links and Hard Links.
Symbolic Links means link between files using file path. For example – command ln -s firstfilepath secondfilepath will create a symbolic link from file at location firstfilepath to file at secondfilepath. If you remove file at location firstfilepath then symbolic link from firstfilepath to secondfilepath will become invalid.
Hard Links means just a second name for same file. It can be created using command ln firstfilename secondfilename. After execution of this command both firstfilename and secondfilename will point to same file location in memory. So if you remove one of file then other filename would still point to same file location in memory.

Below is a picture showing How exactly Hard Links and Symbolic Links are laid inside File System.

Symbolic and Hard links


So removing one part of Hard link doesn’t makes it invalid rather removing one part of Symbolic Link does make it invalid.

ln command in general can be written as ln [options] source target.

Options for ln – File Operations Command

ln Command OptionsDescription
ln -sMakes a Symbolic Link
ln -iInteractive mode, ask user before overwriting destination files
ln -fForce the link, if destination file exists then just overwrite it

ln -s File Operation Command

Makes a Symbolic Link. So for example – if we have two folders Folder1 and Folder2, if Folder1 have file named file1.txt then command ln -s file1.txt filepath of Folder2 will make a new file at location Folder2. But there will be Symbolic Link inbetween these files.

ln -s command

Now If I remove file1.txt from Folder1 and then try to open file1.txt from Folder2, then it would show an error saying “The operation can’t be completed because the original item for “file1.txt” can’t be found.

ln -i File Operation Command

Interactive mode, ask user before overwriting destination files. If file already exists in destination folder then this command before executing will ask if you want to write or not.
So if there’s two folders – Folder1, Folder2 each having file1.txt then command ln -i file1.txt Folder2 FilePath will ask whether to overwrite or not.

ln -i command

ln -f File Operation Command

Force the link, if destination file exists then just overwrite it.
So if there’s two folders – Folder1, Folder2 each having file1.txt then command ln -i file1.txt Folder2 FilePath will just overwrite file1.txt at Folder2.

ln -f Command

Do note that for commands ln -i and ln -f link created between files will be Hard Link not a Symbolic Link.

Final Thoughts

I hope that this article helped you to better File Operations Commands which can be executed in Mac Terminal. Moreover if you have any other question regarding this topic, let me know in comments below and I’ll try to reply to you as soon as possible for me.

Gagan

Hi, there I'm founder of ComputerScienceHub(Started this to bring useful Computer Science information just at one place). Personally I've been doing JavaScript, Python development since 2015(Been long) - Worked upon couple of Web Development Projects, Did some Data Science stuff using Python. Nowadays primarily I work as Freelance JavaScript Developer(Web Developer) and on side-by-side managing team of Computer Science specialists at ComputerScienceHub.io

Leave a Reply

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

Recent Posts