Introduction: Basic Use of Linux Terminal
Welcome to our Instructable!
Here, you will learn the answer to the following prompt:
How to use the Linux Terminal to move/create files, run scripts, create users, change permissions and navigate files and folders.
This is a beneficial skill because Linux is used in many real-life applications. You could apply your knowledge from this Instructable any time you use a computer with Linux on it, in order to make simple tasks involving items like folders and scripts a lot faster and easier. Linux provides lots of shortcuts to make many processes like organization a lot simpler. And if you wanted to, you could use your knowledge of Linux to gain an advantage in a computer-related career.
Hope you enjoy!
Step 1: How to Move a File
The "mv" command for Linux will allow you to move a file from one location (or directory) to another.
Note: mv can be used to rename files as well
Here are the steps in order to use this command:
1. Enter the Linux terminal
2. Type "mv"
3. In the same line, press space and type the name of the current location of the file
4. In the same line, press space and type the name of the location you would like to move the file to
5. Press enter to execute this "command"
After you complete these steps, you should have successfully been able to move your file to your preferred destination.
As you can see, to use this command, you must have three components: "mv" , the current location of the file (in other words, the original file path), and the new location that you'd like to move it to (in other words, the new file path).
Here is an example:
Let's say you want to move a file named "testfile" from your home directory (with the file path home/jack/testfile") to "Documents" (with the file path"/home/jack/documents"). Then, here is what you would type into one line of the Linux terminal:
mv home/jack/testfile /home/jack/documents
**then press enter
If typed correctly, this should work to move the document from its old location to the new one.
What if you wanted to move a number of things at once from one location to another? For example, a large group of files of the same type.
Allow me to show you how to do this by using the following situation:
You have a number of .mp3 files in your ~/Downloads directory
*** NOTE: (~/ – is an easy way to represent your home directory – in our earlier example, that would be /home/jack/)
Now let's say you want to move them in ~/Music. You could quickly move them with this command, like so:
mv ~/Downloads/*.mp3 ~/Music/
The only difference between this and our previous example, is that this time we added a file extension (.mp3) in between our current destination and the new one. By using this command, you will move every file that ends in your chosen file extension from one place to another (in this case, from the Downloads directory to the Music directory). This can make the process of moving files a LOT faster than it would be if you tried to move them individually.
Let's take one final scenario:
What if I want to move a file into the parent directory of my current working directory?
There is an easy way to do this using a shortcut.
Say you have the file "testfile" located in ~/Downloads and you want it in your home directory. If you are currently in the ~/Downloads directory, you can move it up one folder (to ~/) like so:
mv testfile ../
The “../” means to move the folder up one level.
And If you're buried deeper, say ~/Downloads/today/, you can still easily move that file with:
mv testfile ../../
Just remember, each “../” represents one level up.
Note: as you go through these steps, use the "ls" command frequently to make sure you're successfully moving files into the right place. "ls" will list the names of files in the current directory for you. Just type "ls", space, then the name of the file path, and press enter.
Step 2: How to Create Files
Here's how to make simple text files:
First, open the Linux terminal
Next, navigate to the directory where you want to create a file. To navigate to it, type the command "cd", followed by a space, followed by the file path (for example, type "cd /home/wiki/Documents" or "cd Documents"). Press enter to execute the command.
Now, we will use the "cat" command. Type the following command, and press enter:
cat > sample.txt
** and you can replace "sample" with the name you'd like to use for your file.
Now, you may enter the text you'd like to include in your file, then press the "control" and "D" keys on the keyboard to exit the file once you're done.
To verify that the file was saved, type the following command and press enter:
ls -l sample.txt
This will give you a directory listing of your files, and if the name of your text file appears, then it was successfully saved.
What if I only want to create a blank file?
All you have to do is type this:
** replacing "sample" with the name of your choice
Once again, use this command to check that it was saved:
ls -l sample.txt
To add text to this blank file, you will need to use the "vi" command. Here is the format:
Once you do this, you will need to type a lowercase i and press enter to enter editing mode so you can edit the text file.
Type the text you would like, then press the "Esc" key on the keyboard to return to command mode once you're done. And to close vi and save the file, type ":wq" and press enter.
To view the contents of this file, type this and press enter:
Another command that works similar to the right arrow (>) to create an empty text file is the "touch" command. Here is the format to use this:
In order to work with this new blank file, the procedures are exactly the same as explained above. You will use "vi" to edit, "ls" to check if it's saved, and "cat" to view the contents.
Step 3: How to Run Scripts
Here is how to run a Linux "Shell Script" :
First of all, a script will not run by itself by default. You must set execute permissions for your shell script. Enter the Linux terminal, and start by typing this:
$ ls -l script-name-here
Then, to execute the script, type this:
OR if you need to get to the file path first, use this:
**backup.sh remains the same so type this word for word, but path/to/shell/script should be edited to the location of the shell script you would like to run
To run a script called backup.ksh using the ksh shell, type this and press enter:
$ ksh backup.ksh
To run a script called backup.bash using the BASH shell, type this and press enter:
$ bash backup.bash
Here is an example of a process a user may go through to create and run a script:
Start by creating a shell script called hello.sh using a text editor such as vi:
echo "Hello $USER."
echo "Today is $(date)"
echo "Current working directory : $(pwd)"
Save and close the file. Set the permission:
$ chmod +x hello.sh
Run the script:
**If your already in the correct directory, you don't need to type the "./" in the beginning
Step 4: How to Create Users
Here is how to create a simple user on Linux:
This is very simple.
All you have to do is go to the Linux terminal, type this and press enter:
sudo useradd test
**test represents the name of the user-- you can change this to another name you prefer
This command adds a new user called test (or some other name) to your system.
There are a lot of ways you can work with a new users using Linux. For example, you can switch users (using the command ("su -test" , with "su" being short for "superuser"), or change the password of a user (using the command "passwd test").
For more information on other things you can do with Linux regarding users, consult the following link:
Step 5: How to Change Permissions
To start, go to the command prompt, type this, and press enter:
This command lists the contents of the current directory, and the -l "switch" adds a longer listing.
The entries will look similar to this:
-rwxrwxrwx 6 bobsmith users 4096 Dec 9 14:56 test.txt
The “rwxrwxrwx” details the permissions on the “test.txt” file.
The “R” means “read”. This gives permission to view the contents of a file, but not to alter it in any way. The “W” means “write”. This gives permission to change a file or to delete it. The “X” means “execute”. This gives permission to run the file as an executable file. If you want to run an executable file or shell script, it HAS to be marked as executable. So if a file’s permissions are marked “rwx”, that means it can be read, it can be written to/deleted, and it can be executed.
The rwx, as you can see, is repeated three times. The reason for this is because each file/directory has three sets of permissions: one for the file’s owner, one for the owning group, and one for everyone on the system
In order to change the file permissions, we will use the command chmod
To add write permissions to the file's owner, use this command:
chmod a+w test.txt
To add executable permissions to the file's owner, use this command:
chmod u+x test.txt
You can also express these file permissions using numbers. Each permission has a number assigned to it; “R” gets 4, “W” gets 2, and “X” gets 1. The permissions are then added up to determine the permission. For instance, if the file’s owner has a permission of 6, that means he can read the file, write to or delete it, but he can’t execute it as a program.
Therefore, from the example above, we can convert this:
Finally, here is an example for practice: Let's say you wanted to change test.txt’s permissions so that the owner can read, write and execute, the owning group can read and execute, and everyone on the system who is neither the owner nor the owning user can read the file but not write or execute. You would use this chmod command:
chmod 754 test.txt
In this case, the 7 represents the permissions of the owner, the 5 represents the permissions of the owning group, and the 4 represents the permissions of everyone else on the system to use the file.
Step 6: How to Navigate Files & Folders
To navigate through files and folders, you must use the "cd" command.
cd is used to change the "current directory", which is the directory that the user is currently working in
cd syntax is this:
cd [option] [directory (file path)]
**The items in square brackets are optional. cd when used alone will simply return the user to the previous current directory. This provides a convenient means of toggling between two directories.
The following would change the current directory, regardless of where it is on the system (because it is an absolute path), to the root directory (which is represented by a forward slash):
The following would change the current directory, regardless of its location, to the /usr/sbin directory (which contains non-vital system utilities that are used by the system administrator):
If a user currently in directory /usr/local/share/man/ desired to change to the directory /usr/local/share/man/man2, which is a subdirectory of the current directory, it would be possible to change by using the absolute pathname:
Or, you could just use its relative pathname:
On Unix-like operating systems the current directory is represented by a single dot and its parent directory (which is the directory that contains it) is represented by two dots. So, we can change to the parent of the current directory by using the following:
Another convenient feature of cd is the ability for any user to return directly to its home directory by using a single symbol. A Home directory (which is also called a login directory), is the directory on a Unix-like operating system that contains a user's personal files, directories and programs. It is also the directory that a user is first in after logging into the system. A user can return immediately to the home directory by typing the following and then pressing the Enter key:
This is easier than typing the full name of the user's home directory, (like /home/jack)
Step 7: Footnotes
3 tips for the beginning user:
1. Using the "apropos" command, you can find all the commands to do anything. For example, if you want to download a file using a command, type this:
This will show you the list of all the commands, with their initial help, to download a file.
2. It is very important to know about that how can you get the help of any Linux command. You can check the manual page of any command by using the “man” command. Linux Manual pages show the syntax, and give a detailed description of the options of a command. Here is the format:
3. Most of the Linux commands have its help. Other than manual page, this help is also useful to learn the command syntax and options. Sometimes only this help is enough to execute a command, but better look for its manual page to get detailed help. To get help for a command, type this: