Manipulating files and folders with Terminal
In the last article, I tried to show how one can work with files and folders from the Windows command line. If you are more of a Windows user, please be sure to check that article here.
I am going to continue to explain some basic operations one can perform when working with files and folders on Linux OSes. Let's jump into it together by pressing Ctrl + Alt + T to open our terminal.
While in your terminal, whenever you need to clear your screen, press Ctrl+L.
- To know the path of your working directory(folder), execute,
pwd
- To move backwards to the folder that is the host of your working directory execute,
cd ..
Say your working directory is Documents and it has three sub directories and you want to move into one of them say houses, execute,
cd houses
To list all the files and directories(folders) in your working directory execute,
ls
Generally, the syntax for the
ls
command is: ls [ options ] [ /path/to/file ]The options section represents the command line arguments that can be used to manipulate the ouput. Let us see some of those optional arguments.
ls -m
to list directory content seperated by comma(,)ls -Q
to list directory content enclosed by quotation marks("").ls -lh
to display file size in human readable format.ls -g
to omit group ownership column. In a moment you will see what group ownership column is.ls -F
adds a forward slash(/) to directories.ls -i
to display inode number of files and folders(directories).ls -a
to display all files including system hidden files.ls *.
filters files according to the file extension.ls -la
to display all files and directories in long-list format.ls -R
to display files and directories recursively.ls -r
to sorts files in reverse order.ls -X
to sort files alphabetically by file extension.ls -tl
to display files according to file creation date and time.ls -n
to list UIDs and GIDs.ls -t
to sort files by modification date.ls -s
to sort files by file size.ls -v
natural sort of version numbers.ls -l
to display files in long-list format.So this is basically some of the options we can use wrt the syntax
ls [option] [/path/to/file]
so how does this work? i will execute
ls
So I have 9 directories and 2 files in my root(working) directory. So the path of my working directory is, /home/sinke. This means that
ls
command shows just the list of all the content in your working directory.To see more or specific details about the content in your working directory you can use ls command with any of the options listed above. E.g.
ls -l
Let us inteprete this from left to right.
1st Column - File/directory permission.
2nd Column - Number of links.
3rd Column - Name of the owner.
4th Column - Name of the group owner(we mentioned this earlier).
5th Column - File size in bytes.
6th - 8th Columns - Last modification date.
9th Column - File/directory name.
Let's talk about column one a little. For my Documents directory, my column one is, drwxr-xr-x . These are 10 characters, separate them in groups of two; the first character and the last nine characters.
The first character character shows the file type. My first character is d which stands for directory(folder). Values for other file types are:
- Regular files
b Block special file
c Character special file
d Directory
l Symbolic link
n Network file
p FIFO
s Socket
Next nine characters show the file permission. The first three characters are for file permission for the User. The middle three characters are file permission for the group and the last three are file permission for others.
So for my Documents directory as seen above, i have my first column to be dwrxr-xr-x, this means my Documents is a directory(d). The user has permission to read, write and execute(rwx) the Documents directory. The group owners have permission just to read and execute(r-x) the Documents directory. And others have permission to also read and execute(r-x) the Documents directory.
You can use the
chmod
command to change file permissions. The permission characters and their meaning can be:r - permission to read the file.
w - permission to write to the file.
x - permission to execute the file.
s - setgid bit.
t - sticky bit.
Column 3 indicates that sinke is the owner of all the files and directories.
Column 4 indicates that sinke is the group that owns the files and directories.
Column 5 indicates the sizes of the files and directories.
Columnns 6-8 indicates the last modification dates of the files and directories.
And column 9 indicates the name of the file or directory.
To create a new folder/directory say school, in your working directory execute,
mkdir school
You can also create a new folder say school in your working directory and you let school directory to have a subdirectory say class in it. To do this execute,
mkdir school/class
To rename a directoy say Document to say Docku execute,
mv Document Docku
To move a directory say oven from your working directory to say Desktop, do,
mv oven /home/sinke/Desktop
. Noticed how i specified the path?To copy a directory say school from your working directory to another directory say Desktop execute,
cp -r school /home/sinke/Desktop
. Noticed how i specified the path?To delete a folder/directory say school from your working directory do,
rm -r school
. Note: -r is used to delete the directory recursively, which means all its content will be deleted as well.To create a text(specific extension) file say text in your working directory execute,
touch text.txt
To rename this text file from text.txt to say open.txt execute,
mv text.txt open.txt
To move a file say school.txt from your working directory to say Desktop execute,
mv school.txt /home/sinke/Desktop
To copy a file say school.txt from your working directory to say Desktop execute,
cp school.txt /home/sinke/Desktop
.To delete a file say school.txt from your working directory execute,
rm school.txt
Subscribe to my newsletter
Read articles from Enow Sinke directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Enow Sinke
Enow Sinke
Hey there! I'm Sinke, and I'm all about that tech life, especially when it comes to fintech. Currently, I'm on a mission to become a bad-est Full Stack Software Developer, mastering both the front-end and back-end game. But you know what really gets me hyped? AI, baby! I can't get enough of it. I'm super passionate about exploring the endless possibilities it offers and making a mark in this exciting field. When I'm not geeking out over code, you can find me talking shop about Software Engineering and anything else that grabs my attention. I'm always up for a good conversation and love diving into new ideas. Oh, and here's a fun fact about me: I'm a huge fan of Choral Music. There's something magical about those harmonies and melodies that just speaks to my soul. It's my escape from the tech world and a reminder of the beauty of artistic expression. So, join me on this wild tech journey as I chase my dreams and create some serious magic in the world of technology. Let's make waves together!