Skip to main content

Basics: Command Line Practice

This is not required. It is worth 1 point extra credit if you complete the activity and email it to me. This is an OPAL task. Not a task that you do on your own operation system.

Task 01.07 Linux Exercise (optional extra credit)

Launch Command prompt or GitBash (PC) or Terminal (Mac) to log into your Opal account.

Enter each of these commands in order and you will be guided through a helpful practice session: Each step that you take will be recorded in your home directory history file. At the end of this exercise, you will print out your history and paste it into an email and send it to me.

01. Where am I?

Enter the present working directory command:

pwd

pwd shows that you are in your user directory

/fs1/home/youronyen

02. What's in there?

Enter the list command

ls

ls shows you your files and directories

03. How do I clean up the window?

Enter the clear command:

clear

clear clears your screen

04. How can I get back to previous commands?

Press the up arrow key several times to cycle back and see your bash history. (This is saved in a hidden file in your user directory and is named .bash_history.)

05. How do I get back to the command prompt?

Press the down arrow key repeatedly to get back to the blank command prompt.

06. How can I see my history all at once?

Enter the history command to view your bash history:

history

Now you can see the contents of your .bash_history file.

07. How can I clear my history?

First make sure you really want to clear your history. (It's okay to clear your history for this exercise.) Enter the history command followed by a -c clear option:

history -c

Later in this lesson, you will learn how to make a copy, or a backup, of a file. You could do that with your .bash_history if you want to clear the buffer, but save a record of what you did.

If you are interested, here is a lot more info about how to view and make use of your bash history

Enter the history command again to see that your history is now cleared. You should see just one line showing that you entered the history command:

1 history

08. How do I put a directory in the directory that I am in?

Enter the make directory command and then type task-01.07 as name of the directory:

mkdir task-01.07

mkdir must be followed by a directory name. Be sure to use Linux naming conventions.

enter the ls command to see that you now have this directory in your home directory.

09. How do I get out of my home directory and into that subdirectory I just created?

Important! Don't press the return key until directed to do so.

Type, but don't yet enter, the Change Directory Command

cd

then follow the cd by typing a few of the beginning characters of the name of the sub directory that you want to change into ( don't press enter yet.):

cd ta

Now press the tab key to auto complete

cd ta <tab>

bash will finish the typing for you:

cd task-01.07

Now you are ready to press enter.

You used the cd command and the tab autocomplete function to save you time.

If you have two long file names with that start with the same characters, tab will only take you as far as it can on the match. Then you will have to provide some more characters and hit tab again.

10. How do I create a file?

Enter the touch command, a space, and the name of the file you want to create.

touch myfile.txt

Why touch for a name to create an empty file? I have no idea, but I do know that touch has some more options you might need some day, but you definitely don\'t need to know them for this class

Enter the list command to see the file you just "touched" (created) inside the task-01.07 directory.

ls

the directory task-01.07 (should only contain myfile.txt)

11. Can I create a series of files?

Yes, combine the touch command with {} curly brackets or curly braces and any other characters. Try this:

touch {1..5}.txt

this creates 5 files: 1.txt,2.txt,3.txt,4.txt, and 5.txt!

More info on how to create multiple files with bash brace expansion

12. Can I list these files in one column?

Enter the list command and flag the long option

ls -l

ls -llists files in directory in long format and also shows the permissions (we wont go into permissions in this class, but you need to know about them if you want to go to the intermediate stage of linux. Here is a great tutorial on permissions

13. Can I remove a series of files?

Yes. Try this: remove only .txt files that start with 1 through 4:

 rm {1..4}.txt

ls to to confirm you still have 5.txt and myfile.txt

 ls

14. How do I create a hidden file?

Use the touch command and put a dot in front of the file:

touch .hidden

to confirm that the hidden file will not be listed, enter ls without the -a option:

ls

15. How do I list a hidden file?

Show all files by using the -a option with ls (you may see system dot files: . .. that have no additional characters)

ls -a

16. Can I show all files and list them in long format at the same time?

Yes. Show all files and long format in one command ls -la or ls -al order does not matter; both work the same

ls -al

or

ls -la

17. How do I make a subdirectory in a directory?

Make a directory when you are in a directory by using the mkdir command. Try this: make a subdirectory in task-01.07 (you should be there already) named txt-files.

mkdir txt-files

18. How do I move files?

you can move files by using the mv command. You can move one file, or you can move more than one file. You can move all files by using a wild card. Try this: move all (* is a variable that means all or any) of your text files ( that end in .txt) into the txt-files directory.

mv *.txt txt-files

to confirm that txt files are no longer in the task-01.07 directory:

ls

(txt-files directory will be listed, as it is a directory inside of the directory)

to confirm .hidden is still in the directory, list all files

ls -a

19. How can I see my moved files?

You will need to change into the directory you moved them to and then list them:

Change into the txt-files directory using the tab short-cut method. Type cd and then a t and press tab for an auto complete:

cd t (press <tab> so "t" expands to txt-files) 

List all files to confirm that 5.txt and myfile.txt copied and that .hidden did not copy.

ls -a

20. How do I go back one directory?

To go back one directory enter cd ..

cd ..

to confirm you are in the task-01.07 directory enter

pwd

21. How do I copy a file?

Use the cp command to make a copy of a file. Add the name of the file you want to copy after cp, and then add the name of what you want the copy to be named. You can copy hidden or un-hidden files. Try this:

make a copy of your .hidden file.

cp .hidden .hidden2

to confirm copy ls -a

ls -a

22. How do I delete a file?

Use the rm command. Try this:

remove the .hidden2 copy

rm .hidden2

to confirm deletion:

ls -a

23. Can I copy directories?

Yes. Making a copy of a directory is a common way to backup a lot of work. cp must have a -r recursive option declared in order to copy all of the internal files in a directory. Try this to make a backup of your txt-files directory:

cp -r txt-files txt-files-BKUP

This made a backup of your text files directory using the cp copy command and -r recursive option. Directories will not be copied unless you use the -r option!

24. Can I use a remove directory command?

Yes. but rmdir only works on an empty directory. Try this:

Create a temporary directory that you plan to delete:

mkdir txt-files-temp

list the contents of the directory:

ls

Since there are no files in the temp directory, you can delete it with the rmdir command (use the tab shortcut method):

rmdir txt-files-temp

remember rmdir only works on an empty directory!

25. How do I remove a directory that has files?

Use the rm command with the -r option to remove your txt-files-BKUP directory:

rm -r txt-files-BKUP

be extemely careful with this command! For example, if you run this command and option on a html directory, you can wipe out 500 files that took you 300 hours to create.

26. When you get to this point,

Use the history command to print out your history

print in this context does not mean to print to a paper copy to a printer; it means to print it out to the screen following the command prompt.

Select and copy the screen output of the history text, paste it into an email, and send it to me for your optional Task 01.07 extra credit point. This will be a pass fail grade. It is unlikely that you will fail, but if you do, you may redo this task and resubmit with no penalty.