Linux¶
How to list the contents of a directory¶
The ls command is used to list the contents of a directory in Linux. It's a very helpful tool for organizing and locating your files.
Here's an example of how to use the ls command:
Open a terminal window on your Linux machine.
By default, if you type
$ ls
and press enter, you will see a list of all the files and directories in the current working directory.
There are several options you can use with the ls command to get different information or change the way the information is displayed. Here are a few examples:
-
ls -l: This option shows a detailed listing of the files and directories, including information like permissions, owner, group, size, and date modified.
-
ls -a: This option shows hidden files and directories in addition to the regular files.
-
ls -t: This option sorts the files and directories by the time they were last modified, with the most recently modified files appearing first.
You can also combine options. For example, ls -lt will show a detailed listing of the files and directories, sorted by the time they were last modified.
Using the ls command with different options can help you quickly find the information you're looking for, and it's a great way to get started with learning more about the command line in Linux.
How to create folders and change the current directory¶
Let's say you want to create a new directory called "project" and then change into that directory to work on some files. Here's how you would do that using the mkdir and cd commands in Linux:
Open a terminal window on your Linux machine. This is where you will type in commands.
Type
mkdir project
and press enter. This will create a new directory called "project".
Type
cd project
and press enter. This will change the current working directory to the "project" directory.
Now, you should be inside the "project" directory and you can start adding, editing, or deleting files as you need. To check that you're in the right place, you can use the pwd command to print the name of the current working directory.
That's it! These two commands are a great way to get started with organizing your files and directories in Linux.
How to copy files and folders¶
To copy files and folders in Linux, you can use the cp command.
Here's an example to help you understand how to use this command:
Open a terminal window on your Linux machine.
Let's say you have a file called "file1.txt" in your current directory and you want to make a copy of it called "file2.txt". To do this, type
cp file1.txt file2.txt
and press enter.
If you want to copy a directory and all its contents to a new location, you can use the -r option. For example, let's say you have a directory called "myfolder" and you want to copy it to a new location called "newlocation". Type
cp -r myfolder newlocation
and press enter.
You can also copy a file or directory to a different location by specifying the destination path. For example, let's say you have a file called "file1.txt" in your current directory and you want to copy it to a directory called "newfolder" in the home directory. Type
cp file1.txt ~/newfolder/file1.txt
and press enter.
That's it! These simple examples should give you a good understanding of how to use the cp command to copy files and directories in Linux.
How to move (or rename) files and folders¶
To move files and folders in Linux, you can use the mv command.
Here's an example to help you understand how to use this command:
Open a terminal window on your Linux machine.
Let's say you have a file called "file1.txt" in your current directory and you want to move it to a new location called "newfolder". To do this, type
mv file1.txt newfolder/file1.txt
and press enter.
If you want to rename a file, you can use the mv command as well. For example, let's say you have a file called "file1.txt" and you want to change its name to "file2.txt". Type
mv file1.txt file2.txt
and press enter.
If you want to move a directory and all its contents to a new location, you can use the mv command just like you would with a file. For example, let's say you have a directory called "myfolder" and you want to move it to a new location called "newlocation". Type
mv myfolder newlocation/myfolder
and press enter.
That's it! These simple examples should give you a good understanding of how to use the mv command to move files and directories in Linux. Keep in mind that when you move a file or directory, it's permanently removed from its original location.
How to remove files and folders¶
The rm command is used to delete files in Linux. However, if you want to delete a directory and all the files inside of it, you need to use the rm -r command.
Here's a simple example to help you understand how to use these commands:
Open a terminal window on your Linux machine.
Let's say you have a directory called "myfiles" with several files inside of it. To delete this directory and all its contents, type
rm -r myfiles
and press enter.
If you only want to delete a single file, for example "file1.txt", type
rm file1.txt
and press enter.
It's important to be careful when using the rm and rm -r commands because once a file or directory is deleted, it's gone for good. There is no recycle bin or trash folder in Linux like there is in other operating systems.
So, before you use these commands, make sure you know exactly what you're deleting and that you don't need the files anymore. You can use the ls command to list the contents of a directory and check what you're about to delete.
Linux commands¶
-
ls - This command is used to list the contents of a directory.
-
cd - This command is used to change the current working directory.
-
pwd - This command is used to display the current working directory.
-
mkdir - This command is used to create a new directory.
-
rmdir - This command is used to delete an empty directory.
-
cp - This command is used to copy files or directories from one location to another.
-
mv - This command is used to move or rename files or directories.
-
rm - This command is used to delete files or directories.
-
cat - This command is used to display the contents of a file on the terminal.
-
less - This command is used to display the contents of a file in a paginated format, one screen at a time.
-
echo - This command is used to display a message on the terminal or to write data to a file.
-
grep - This command is used to search for a specified text pattern in a file or in the output of another command.
-
find - This command is used to search for files or directories based on various criteria such as name, size, type, etc.
-
tar - This command is used to create or extract archive files in the tar format.
-
chmod - This command is used to change the permissions of files or directories.
-
chown - This command is used to change the owner or group of a file or directory.
-
ping - This command is used to test the network connectivity to a specified host.
-
ssh - This command is used to securely log in to a remote machine and execute commands on it.