القائمة الرئيسية

الصفحات

Create files and directories in Linux

Commands for creating files and directories in Linux 


Let us now see how to create files and directories in Linux


Creating directories 


- First of all, we will see how to create a directory


- To create a directory, we have the mkdir command, which stands for make directory


- By running this command followed by a directory name a new directory will be created


- For example here if we run mkdir python, a new folder called python will be created


- the directory will be created in the current directory by default, if  you to create the directory in an other location, just specify either the absolute path or the relative path to the location where you want to create this new directory


- You can create multiple folder at the same time, by specifying multiple names separated by a space to the mkdir command


- let's say that we want to create a parent directory and a child directory at the same time, what should we do to achieve something like that?


- for that we have to use the -p option, which will ensures that any intermediate directories in the path that do not exist, will be created as well


- for example here, by running mkdir -p javascript/js-v1, a folder called javascript will be create and inside this folder, a folder called js-v1 will be created as well


Creating directories in Linux



Creating files


- Let us now, see a very simple way to create files in Linux 


- to create a file in linux, you can use the touch command followed by the path where you want to create this file


- For example here by running, touch html/file.html, a file called file.html will be created in the html directory


- and we can verify that the file was created by running ls html to list the content of the html directory


- Let us also create two other files in the html directory, one called app.py and another called styles.css, that we will need later to work with


Creating files in Linux



moving and renaming files


- now you see that we have different files of different types, all of them in the html directory. so let's see how we can put them in the right place by moving each file to the appropriate directory


- In linux, you can move a file or a directory from one location to another using the mv command which stands for move


- For example to move the app.py to the python directory, all we have to do here, is to run the mv command and provide to it the name of the file and the destination


- The same if we want to move the styles.css file to the css directory, so just we have to run mv followed by the filename which is html/styles.css followed by the destination 


- if a file with the same name already exist in the destination directory, the mv command will overwrite this file. and in this situation, you have to provide the -f option to the mv command to enforce moving the file.


- And you can also move multiple files at the same time by providing multiple filenames to the mv command


- we can use the mv command to rename files as well


- Here for example, we can rename the html file, by running the mv command followed by the old filename which file.html followed the new filename which is index.html


Moving and renaming a file or a directory in Linux



Coping files and directories


- Let us now, see how to copy a file or a directory


- To copy a file or a directory from one location to another, we can use the cp command which stands for copy


- For example to copy the app.py file to the python3 directory, here we have to run the cp command followed by python/app.py which is the path to the file, followed by python3/ which is the destination


- if you want to copy a directory with all its content to a new location, here you have to know that the -r option is required


- For example, to make a copy of the python directory and its content in the code directory, here we have to provide the -r option to the cp command in addition to source_file and the destination


- And like the mv command, if a file with the same name already exists in the new location, here you have to run the cp command with -f option


- and you can copy multiple files at the same time, by providing multiple filenames to the same cp command


Copying a file or a directory in Linux



Removing files and directories


- Finally, we will see how to remove files and directories in Linux


- we can remove files in linux by running the rm command, which stands for remove, followed by the name of the file


- For example to remove the app.py file in the python directory, all we have to do here, is to run rm /python/app.py


- and for removing a directory, we have the rmdir command. But this directory should be empty. for example to remove the python directory, we can do that by running rmdir followed by the directory name, which is python in our case


- But what if we want to remove a directory that is not empty?? So how we can remove a directory with its entire content?


- if you try to use the rmdir command for that, you will get en error telling you that this directory is not empty


- to remove a directory with its content, you have to use the rm command with -r option followed by the name of this directory


- For example here, we can remove the python3 directory, which contains a file called app.py, by running rm -r python3


Removing files or directories in Linux



So that's it for now, try to work with these commands and see you in the next lecture

Comments