Basic

Published on November 2016 | Categories: Documents | Downloads: 22 | Comments: 0 | Views: 209
of 20
Download PDF   Embed   Report

Comments

Content

[email protected] | My favorites ▼ | English ▼ | Sign out Skip to page content Skip to main navigation

e.g. "adwords" or "open source"

Google Code University
    

  

Home Tutorials and Introductions Courses o Programming Languages o Web Programming o Web Security o Algorithms o Android o Distributed Systems o Tools 101 o Google APIs & Tools Discussion Forums Submit a Course Curriculum SearchNew!

More Google Code University resource links

Basic Linux Commands
Table of Contents
Overview Prerequisites Learning objectives Conventions Introduction The Linux File System Getting Command Line Access The Command Line Determining your working directory What is in a directory?

Making a new directory Change your working directory Create a new file Differences between files and directories Copy a file into another directory Removing files and directories Moving and renaming Finishing Up Additional Examples and Exercises

Overview
This lab is written for people with little to no prior experience using the Linux command line, but who have some familiarity with Windows.

Prerequisites
 

A user account with a home directory on a Linux machine. Any standard, major Linux distribution (and most minor ones, too) should be fine. Either physical or remote access to the Linux machine and the ability to log into the machine. Physical access is best and easiest, but remote access works great too.

Windows users: You must have an SSH-capable terminal program, such as Putty, installed on your machine so you can log into the Linux machine using SSH.

Learning objectives
Upon completion of this lab, you will be able to:
 

Outline the benefits of using a command line interface Manipulate files and directories from the command line on a Linux system

You will have practiced:
     

showing the contents of a directory making a new directory changing from one directory to another copying files and directories moving and renaming files and directories removing files and directories.

Conventions

 

Courier font indicates screen output (command results, messages, and prompts that the system displays) Courier Bold font indicates commands that you enter

Introduction
For most personal computer (PC) users, moving a mouse around a graphical user interface (GUI) is intuitive, easy, and less intimidating than working on a command line interface (CLI). However, operating a machine from a CLI provides quicker access to the real power of the operating system (OS). All of the major PC operating systems (MS Windows 2000/XP/Vista, Mac OS X, Linux, UNIX) provide a CLI. In Linux and many other operating systems, this is called the "Shell". Because Linux is so adaptable, some advanced users even like to use shells other than the default one! We'll focus on the shell most Linux distributions use by default, which is called "Bash". (It's an acronym with a long story behind it.) For many types of activities, using the CLI can have a number of advantages over using a GUI. Advanced computer users often work in the CLI to
     

gain more control the file system and operating system execute multiple commands more quickly than they could in the GUI easily script a sequence of commands to perform tasks and execute programs get started more quickly and use fewer system resources than when working in the GUI gain remote access to other devices that may not be available from the GUI work on a machine remotely over the internet or other network connection (A CLI is much faster because it does not have to transmit graphical information, such as when you use VNC)

This lab will help you learn the basics of using Linux commands by practicing some common tasks: copying, moving, renaming, and deleting files. Yes, it's true that you can just do this with a GUI, but by getting used to using the terminal, you will always be able to work on your machine even if you must work remotely. Learning the basics also helps lay the foundation for learning more advanced tasks in Linux as you continue to learn with later labs and other experiences. Before we move on, can you think of any other tasks where a CLI would possibly be faster than using a GUI?

The Linux File System
The Linux file system has some similarities to Windows and some differences. For example, the path to one of the sample pictures in a default Windows XP installation is:
C:\Documents and Settings\yourusername\My Documents\My Pictures\Sample Pictures\Winter.jpg.

The path to the picture begins with C:, an identifier that represents a device - in this case, a hard drive. A Windows path may begin with a different drive letter such as A: or E: and each letter

represents a specific device, for example, a hard drive or CD-ROM reader. The backslash ( \ ) following the device identifier, indicates the "root directory file" (top level directory) of that device. Each device on the computer has its own root to which directories and files are "attached". In Linux, paths to everything (not just directories and files) begin at the root directory. Think of the Linux file system as a tree with a single trunk and many branches. All devices, system files, user-created files, and directories are attached to the branches of the Linux root directory. In the Linux file system, the root directory (top level) of the file system is represented as a forward slash ( / ). If you are familiar with Windows, you see a similarity with the "My Computer" folder. The My Computer folder acts like a starting point from which you can access any storage device. A path to a similar file on a Linux system might look like this:
/home/yourusername/pictures/winter.jpg

Two things to note about this example:
 

Linux uses a forward slash ( / ) instead of a backslash ( \ ) The picture file doesn't exist on your Linux machine unless you put it there yourself.

...and now, we can get started!

Getting Command Line Access
If you do not have physical access to a Linux system and you are connecting using Putty or a similar program, you will need to know the hostname of a remote Linux machine you can log into. Perhaps it is a friend's computer, or you might be paying for a hosted server. Ask the owner of the machine for the details you need in order to log in. Do one of the following, depending on which type of computer you are using to access your Linux computer. Linux In the GUI environment of a Linux machine, open a Terminal from your Applications menu. If you are not using the GUI, then you are probably looking at a terminal with a command line prompt already. OS X Use the Terminal application to open a shell, and then use the ssh command to connect to a remote Linux machine. Much of what you learn in this lab can also be applied to the Mac OS X command line, but there are some differences here and there.

Microsoft Windows Run an SSH client program (like Putty) to connect to the remote Linux machine where you have an account.

The Command Line
Once you login, you will see a prompt that looks something like this in your console (terminal window):
computer:~$ _

or this:
user@computer:~$ _

The prompt gives you quite a bit of information:
   



user This is your login name. computer This is the name of the machine you are logged into. :~ This shows the current directory. The tilde by itself indicates that you are in your home directory (which is where you keep all of your own files). $ This is the prompt. If you see some other characters such as #, you might be logged in with special privileges. Log out (by typing "logout"), and then back in, making sure to use your username and not "root". _ The underscore in our example is either a flashing cursor, an underscore, or a block on your machine. This is where you type your commands.

For this lab, we'll use an example user (ninja) and computer (ultimatepower). You should see your own username and the name of the machine you logged into in your terminal window (unless, of course, you do actually possess Real Ultimate Power...) Command syntax The syntax used for commands in this lab is: command \[options\] filename Note: Options are also called "flags". Pressing the Enter key executes a command.

Determining your working directory
To find out what directory you are currently working in, use the pwd (Print Working Directory) command.
ninja@ultimatepower:~$ pwd

/home/ninja

The pwd command displays the full name of the directory in which you are working. The name of a user's home directory is his or her username, so yours will be different from the example (ninja is the person's username and the path to her home directory is /home/ninja.) Following is a diagram of the directory structure (tree):
home >> ninja

In these diagrams, the directory we are currently working in will be bold.

What is in a directory?
To find out what's in a directory, you can use the ls command to list its contents. Typing the command by itself lists the contents of your current working directory, but you also can use the command to list the contents of any directory. For now, just type ls and press Enter. For a new user account, you will see something like the following:
ninja@ultimatepower:~$ ls Desktop

The result is a single filename, in this case, a directory named Desktop, and that's it. The diagram for the file location is:
home >> ninja >> >> Desktop

The ls command has shown us all visible files, but to be sure you are seeing everything contained in this directory, type the ls command, then add a space and the "all" flag, -a, then press Enter.
ninja@ultimatepower:~$ ls -a . .. .bash_logout .bash_profile .bashrc Desktop

The result contains an entry for a single dot, a double dot, and three new files in addition to the directory file we saw before. The new files all have something in common: their names start with a dot. What is all of this? Single dot - Current Directory A single dot is an "object" that represents the current directory, your "working directory". If you are working in /usr/lib/ then the single dot represents /usr/lib ; if you are working in

your home directory, the dot represents your home directory. Compare the output from entering the following variations of the ls command while working in the user's home directory:
ninja@ultimatepower:~$ ls Desktop ninja@ultimatepower:~$ ls /home/ninja/ Desktop ninja@ultimatepower:~$ ls . Desktop

The diagram for this is:
home >> ninja >> >> Desktop

Notice that the output of all three commands is the same. (Did you also notice in the second example above, we typed the full path to the current directory?) Double dot - Parent Directory the double dot (two dots) is an object that represents the relative parent directory. This represents the directory immediately "above" (or which contains) the current directory. In the example, "home" is the parent of "ninja" in /home/ninja/. Type the ls command, a space, and the two dots, as shown, then press Enter. This will list the contents of the parent directory for your current working directory.
ninja@ultimatepower:~$ ls .. ninja

The contents of your parent directory (/home) is "ninja". Notice that using the ls command with the name of the parent directory, in this case /home, achieves the same result.
ninja@ultimatepower:~$ ls /home ninja

Leading dot - Hidden File A leading dot before a file name means that the file is "hidden" and won't be displayed when you use ls without a flag. Type the ls command, press Enter and view the result.
ninja@ultimatepower:~$ ls Desktop

Now enter ls -a to see all files and directories, including hidden ones (the output below is an example; you may see different files.)
ninja@ultimatepower:~$ ls -a

. .. .bash_logout .bash_profile .bashrc Desktop

Using cat to see file contents Linux programs frequently use hidden files to save personalized settings. These "config files" are typically standard text files. You can use the cat command (short for "concatenate") to display the contents of files, including hidden ones. Type cat .bash_logout and then press Enter to display the contents of the .bash_logout file. If you do not have that file, use the cat command to display another file in your directory. Note that the contents of the file you display will likely be different from the output below.
ninja@ultimatepower:~$ cat .bash_logout # ~/.bash_logout: executed by bash(1) when login shell exits. # when leaving the console clear the screen to increase privacy if [ "$SHLVL" = 1 ]; then [ -x /usr/bin/clear_console ] && /usr/bin/clear_console -q fi

Although the contents of the file may not make sense to you right now, you can see that a hidden file is like any other file, except that the leading dot in the name prevents it from being displayed when the ls command is used without a flag. Quick review Take a minute to try the commands you just learned to examine the files in your home directory. Do you remember how to get the path to your home directory? (Hint: re-read Determining Your Working Directory.) Explore your home directory using ls and ls -a.
 

What files do you see? If you've been using your system for a while, you might have a large number of files! If you have files, use the cat command to take a look at some of the files with leading dots.

Explore your directory structure using ls and ls -a:  Type ls .. (ls, space, and two dots) to see what's in the parent directory. What do you find?  What do you see if you add the -a flag to ls?

Making a new directory
Use the mkdir command to make a new directory using an unique name and then use ls to verify that your new directory was created.

Before:
ninja@ultimatepower:~$ ls Desktop

Make your new directory:
ninja@ultimatepower:~$ mkdir more_cowbell

After:
ninja@ultimatepower:~$ ls Desktop more_cowbell

If you try to create a new directory and give it a name that is not unique for that working directory, you'll see the following error:
ninja@ultimatepower:~$ mkdir Desktop mkdir: cannot create directory `Desktop': File exists

If you successfully created the new directory, your directory structure will now be similar to this:
home >> ninja >> >> Desktop >> >> more_cowbell

Change your working directory
In Linux, you use the cd command to change your working directory. You can specify a directory change in several ways. Typing the cd command and pressing Enter will take you to your home directory from anywhere on the system. For example:
ninja@ultimatepower:/usr/lib$ cd ninja@ultimatepower:~$ pwd /home/ninja

Typing cd and the full name of a directory will take you to that directory from anywhere on the system. Full directory names begin with a leading forward slash. For example:
ninja@ultimatepower:~$ cd /home ninja@ultimatepower:/home$ pwd /home

Using cd with a partial directory name can take you to that directory, provided the directory is directly below your current working directory. Partial directory names do not start with a leading slash.
ninja@ultimatepower:/home$ cd ninja@ultimatepower:~$ cd more_cowbell ninja@ultimatepower:~/more_cowbell$ pwd /home/ninja/more_cowbell

A cd followed by two dots will take you up one level to the parent directory of your current working directory.
ninja@ultimatepower:~/more_cowbell$ cd .. ninja@ultimatepower:~$ pwd /home/ninja

To change your working directory to the new directory you created (more_cowbell), type the cd command and specify either a full directory name or a partial one (if it's below your current working directory). If you get stuck, you can use the cd command to go to your home directory, and then cd more_cowbell again to get to more_cowbell.) Type the ls command to display the contents of the newly created directory. Note that there are no files displayed.
ninja@ultimatepower:~/more_cowbell$ ls

Type the ls -a command to display any hidden files (there are none) and to show the current and parent directory objects ( . and .. ).
ninja@ultimatepower:~/more_cowbell$ ls -a . ..

Create a new file
There are many ways to create and edit text files with Linux, most notably the vi and Emacs text editors. Both editors have a bit of a learning curve, so you should use either the Pico or the Nano text editor (they are very similar) for this exercise. To find out if you have Pico, at the command line, enter pico music.txt and then press Enter.
ninja@ultimatepower:~/more_cowbell$ pico music.txt

If you get a "command not found" error, try entering nano music.txt instead.

If you don't have either of these editors, Google for a vi tutorial and use the vi editor. Using the Pico editor The Pico editor appears in your console (window). You'll see something like this (with a bunch more space between the top and bottom):
UW PICO(tm) 4.6 File: music.txt

. . . [ New file ] ^R Read File ^Y Prev Pg ^W Where is ^V Next Pg

^G Get Help ^X Exit

^O WriteOut ^J Justify

^K Cut Text ^C Cur Pos ^U UnCut Text^T To Spell

At this point, you can enter text. Type in two or three of your favorite bands or musicians. If you have none (or just have a case of performance anxiety), feel free to use this example.
UW PICO(tm) 4.6 Polyphonic Spree The Go! Team Janis Joplin File: music.txt

^G Get Help ^X Exit

^O WriteOut ^J Justify

[ New file ] ^R Read File ^Y Prev Pg ^W Where is ^V Next Pg

^K Cut Text ^C Cur Pos ^U UnCut Text^T To Spell

Look at the menu of Pico commands at the bottom of your screen. Find the one for Exit. The command for Exit is ^X which is shorthand for "Control Key (ctrl) plus the letter X". When you are finished entering band names, hold down the ctrl key and type an x. A t the bottom of your screen you will see a prompt asking if you want to save your work (the "modified buffer"). Type a y (you do not need to press Enter).
Save modified buffer (ANSWERING "No" WILL DESTROY CHANGES) ? Y Yes ^C Cancel N No

The next prompt asks what file name to use. To keep things simple, just press Enter to use the file name you already created.

File Name to write : music.txt ^G Get Help ^T To Files ^C Cancel TAB Complete

You are returned back to your command prompt (Home, sweet home!). Now that you have a file in your new directory, use a ls command to see it.
ninja@ultimatepower:~/more_cowbell$ ls music.txt ninja@ultimatepower:~/more_cowbell$ ls -a . .. music.txt

To see what the contents of the file looks like, type the cat music.txt command and press Enter. Now that you are a Pico (or nano) veteran, create a file named classical.txt in the more_cowbell directory and add the following composers (one per line):
  

Bach Brahms Beethoven

Of course, you probably noticed that the list is not in the correct chronological order. See if you can figure out how to cut "Brahms" and paste it after "Beethoven" using the control commands at the bottom of your screen for help. (Hint: Look for Cut and Uncut instead of Cut and Paste.) To learn more about your text editor commands, you can type Control + G to display the online help.

Differences between files and directories
Since the command prompt obviously lacks the pretty, 16,000-color folder icons that a GUI might provide, we need a way to distinguish which of the items listed in ls are files and which are directories. As with many things in Linux, we have plenty of options to choose from. You can choose whichever method you like best. To explore your identification options, create a new directory in more_cowbell.
ninja@ultimatepower:~/more_cowbell$ mkdir songs

Color Schemes To see if your console automatically supports color (most do), type a ls command and press Enter

ninja@ultimatepower:~/more_cowbell$ ls classical.txt music.txt songs

If your console supports colors, the output of ls should be color-coded. If the output is not in color, use ls and add the --color option.
ninja@ultimatepower:~/more_cowbell$ ls --color classical.txt music.txt songs

Normal files appear in the normal text color, directories are blue, and executable programs are green (or another color if your console text is normally green). Be aware that the colors used may vary from one system to the next. Classify option Adding the -F option to the ls command adds a symbol to the end of some listings, indicating what type of entries they are. Directories will be listed with a trailing slash (note: be sure to use the uppercase letter F).
ninja@ultimatepower:~/more_cowbell$ ls -F classical.txt music.txt songs/

Long list format The -l (lowercase L) option for the ls command displays lots of information about each file. In the example below, a "d" at the start of a line indicates a directory. You will learn more about this additional information in the upcoming Ownership and Permissions lab.
ninja@ultimatepower:~/more_cowbell$ ls -l total 8 -rw-r--r-- 1 ninja ninja 70 Apr 23 17:50 classical.txt -rw-r--r-- 1 ninja ninja 37 Apr 23 17:44 music.txt drwxr-xr-x 2 ninja ninja 4096 Apr 23 17:58 songs

If you are familiar with other computer systems, you might assume that the listings that have file extensions (such as, music.txt) are regular files, and those without extensions are directories. If so, you may be surprised to learn that a Linux system treats a dot just like any other character in the filename. The exception to this is when a dot is the first character of a filename (as you saw earlier, a leading dot in a filename makes the file hidden). A directory can have a dot in its name, and a regular file need not have any extension. Linux, unlike Windows, does not rely upon extensions to determine how a file should be treated. For example, create a new directory with a .txt extension and then use each of the three methods as shown to display the contents of your working directory:
ninja@ultimatepower:~/more_cowbell$ mkdir songs.txt ninja@ultimatepower:~/more_cowbell$ ls --color

classical.txt music.txt appears in blue!

songs

songs.txt

//Note that songs.txt

ninja@ultimatepower:~/more_cowbell$ ls -F classical.txt music.txt songs/ songs.txt/ appears with a following slash!

//Note that songs.txt

ninja@ultimatepower:~/more_cowbell$ ls -lh total 12K -rw-r--r-- 1 ninja ninja 70 Apr 23 17:50 classical.txt -rw-r--r-- 1 ninja ninja 37 Apr 23 17:44 music.txt drwxr-xr-x 2 ninja ninja 4.0K Apr 23 17:58 songs drwxr-xr-x 2 ninja ninja 4.0K Apr 23 18:13 songs.txt //Note that songs.txt appears with a "d" at the beginning of its listing.

To further confirm that songs.txt is indeed a directory,you can use the cd command to go into it and the ls -a command to view its contents.
ninja@ultimatepower:~/more_cowbell$ cd songs.txt ninja@ultimatepower:~/more_cowbell/songs.txt$ ls -a . ..

There seem to be about four bajillion (not a real number!) options for the ls command, but how do you find out about them all? True, Google has a search engine you can use, and yes, Wikipedia does help sometimes. However, the easiest way to get Linux command information is from the help manual, also known as man for short (and for ease of typing!). To use the help manual, type man, a space, and the command you want to know about. To see the manual for the ls command, type man ls and press Enter. To explore the man pages for the command, use the space bar to advance to the next section of the "man page". When you are through reading you can type q to exit. Self-study With the man page for ls open, find out how to do the following tasks, and then try them out.
    

Do a long listing and include the size of each file in human-friendly units like megabytes or kilobytes Do a long listing and include the author of each file Sort the directory list by file size Sort the files in reverse order List one file per line

Copy a file into another directory

Now that you've created some files, you are ready to learn how to copy a file in Linux. First, to make sure you are in the the "more_cowbell" directory you created earlier, type cd ~/more_cowbell and then press Enter.
ninja@ultimatepower:~$ cd ~/more_cowbell ninja@ultimatepower:~/more_cowbell$

You changed your working directory to more_cowbell. Notice that this time you used a typing shortcut - the tilde (~) character - instead of typing the name of your home directory. There are lots of ways to avoid typing in Linux. ^_Verify the existence of the files and directories you created earlier:
ninja@ultimatepower:~/more_cowbell$ ls -F classical.txt music.txt songs/ songs.txt/

In Linux, the copy command is cp, a shorthand for "copy". Note that typing cp instead of copy saves us two whole letters of typing! (Seriously, Linux people really like to avoid any unnecessary typing. ^_- ) To copy a file with the cp command, you have to specify the file you want to copy (the source file) and the destination (location and/or filename) to where you want to copy it. The destination can be a filename, a directory, or a directory and a filename. Type the cp command and two filenames as shown and then press Enter.
ninja@ultimatepower:~/more_cowbell$ cp music.txt copymusic.txt

When you list your files, you can see that you made a new file called copymusic.txt. What you "said" in Linux was "copy the music.txt file to a new file in this same directory, and name the new file copymusic.txt."
ninja@ultimatepower:~/more_cowbell$ ls -F classical.txt copymusic.txt music.txt songs/ songs.txt/

Now type the cp command, one filename, and one directory name as shown, and then press Enter.
ninja@ultimatepower:~/more_cowbell$ cp music.txt songs/

What you "said" in Linux was, "copy the file music.txt into the songs directory, without changing the name of the file". When you list the contents of the songs directory, you can see that you made a copy of music.txt in that directory.
ninja@ultimatepower:~/more_cowbell$ ls -F songs/ music.txt

Now type the cp command, one filename (music.txt), and the full path of a new file as shown, and then press Enter.
ninja@ultimatepower:~/more_cowbell$ cp music.txt /home/ninja/my_music.txt

What you "said" in Linux was, "copy the file music.txt to the /home/ninja/ directory and name the new file my_music.txt". List the directory contents to see the new file.
ninja@ultimatepower:~/more_cowbell$ ls -F ~ Desktop/ more_cowbell/ my_music.txt

Note that when we copied a file into a subdirectory of our working directory (cp music.txt songs/), we added a trailing slash to the directory name. The slash specifies that the destination name is a directory, not a file. Although it is optional to include the slash, remembering to use the slash can prevent you from accidentally overwriting a file. Type the cp command, and the two filenames as shown, and then press Enter.
ninja@ultimatepower:~/more_cowbell$ cp music.txt copymusic.txt ninja@ultimatepower:~/more_cowbell$

In this example, the original copymusic.txt file got overwritten by the music.txt file. Of course, you don't have to be concerned about hurting an example file, but what if that had been an important fie of yours? Ouch! This time, type the cp command, and put a slash after copymusic.txt, and then press Enter.
ninja@ultimatepower:~/more_cowbell$ cp music.txt copymusic.txt/ cp: accessing `copymusic.txt/': Not a directory ninja@ultimatepower:~/more_cowbell$

In the second example, you are informed that the destination isn't a directory, and the unwanted copy operation (accidentally copying the file onto another that exists) does not take place.

Removing files and directories
Now that you have done all of that copying, you have a lot of example files scattered about that you do not really need so it's time to delete a few of them. Change to the home directory by typing cd and then pressing Enter, then type ls and press Enter to list the contents of the directory.
ninja@ultimatepower:~/more_cowbell$ cd ninja@ultimatepower:~$ ls Desktop more_cowbell my_music.txt

Type rm, a space, and the name of the file you want to delete, in this case, my_music.txt, and then press Enter. To verify that the file was deleted, type ls and press Enter to list the contents of the directory.
ninja@ultimatepower:~$ rm my_music.txt ninja@ultimatepower:~$ ls Desktop more_cowbell

Caution! The rm command is very powerful. Once you remove a file, recovering that file can be very difficult, if not impossible. The command line does not provide the safety of "are you sure?" questions like the GUI interface does and there is no "unremove" or "undelete" command. Next step in your clean up is to remove the songs.txt directory you created. In general, directories cannot be deleted with the rm command (there is an exception which will be covered in a later section of this course). The normal command for removing a directory is rmdir, which you can remember as "remove directory". You can only remove a directory that is empty with rmdir. Change your working directory to more_cowbell, as shown, then list the files with categories.
ninja@ultimatepower:~$ cd more_cowbell ninja@ultimatepower:~/more_cowbell$ ls -F classical.txt copymusic.txt music.txt songs/ songs.txt/

The ls -F command shows the directories marked with trailing slashes (songs/ and songs.txt/). Type the command for removing the songs.txt directory, as shown, then list the files with categories.
ninja@ultimatepower:~/more_cowbell$ rmdir songs.txt ninja@ultimatepower:~/more_cowbell$ ls -F classical.txt copymusic.txt music.txt songs/

The songs.txt/ directory has been removed. Use rm to remove the extra copy of the music.txt file, copymusic.txt, then list the remaining files, as shown.
ninja@ultimatepower:~/more_cowbell$ rm copymusic.txt ninja@ultimatepower:~/more_cowbell$ ls classical.txt music.txt songs

Use rm to remove the classical.txt file (it doesn't belong in this directory since those composers usually didn't include cowbell sections in their orchestras).
ninja@ultimatepower:~/more_cowbell$ rm classical.txt ninja@ultimatepower:~/more_cowbell$ ls music.txt songs

Moving and renaming
Sometimes you may want to rename a file. For example, you might want to rename your list of bands musicians.txt instead of music.txt. Though the Bash shell does not have a rename command, you can achieve the same effect of a rename by "moving" a file from one name to another. The syntax (grammar) of the move command is mv <source> <destination>. Begin by listing the files in the directory using ls. Type the move command, the name of the original file, and the name you want to change it to (the new filename), as follows: mv music.txt musicians.txt.
ninja@ultimatepower:~/more_cowbell$ ls music.txt songs ninja@ultimatepower:~/more_cowbell$ mv music.txt musicians.txt

List the files in the directory to make sure you changed the original filename to the one you wanted.
ninja@ultimatepower:~/more_cowbell$ ls musicians.txt songs

As you might expect, you can use the mv command to move a file from one location in the directory tree to another. Move the musicians.txt file into your home directory by typing mv, a space, the filename you want to move (musicians.txt), a space, then the full path of the directory you want to move the file to (/home/ninja/).
ninja@ultimatepower:~/more_cowbell$ ls musicians.txt songs ninja@ultimatepower:~/more_cowbell$ mv musicians.txt /home/ninja/

Use the ls command to see the results of the mv command in the more_cowbell directory; the musicians.txt file is no longer there.

ninja@ultimatepower:~/more_cowbell$ ls songs

Now use the ls command to see the results of the mv command in the /home/ninja/ directory; the musicians.txt file is now in that directory.
ninja@ultimatepower:~/more_cowbell$ ls /home/ninja/ Desktop more_cowbell musicians.txt

To finish the cleanup exercise, you will want to change your working directory to your home directory and wipe out everything in the more_cowbell directory, and the directory itself, with one command. For this, you will use the rm command with the -r , or recursive, option. Using rm with the recursive option causes the command to travel down the directories and remove everything. This is a powerful command that one need be careful with! Caution! Adding the -r option to the rm command makes it even more powerful. Remember that there is no "unremove" or "undelete" command so check to make sure that you type the command and directory entry correctly before you press Enter.
ninja@ultimatepower:~$ ls -F Desktop/ more_cowbell/ musicians.txt ninja@ultimatepower:~$ rm -r more_cowbell ninja@ultimatepower:~$ ls Desktop musicians.txt

Remember the man pages you used earlier to look up the options for ls? Use man rm to find what option to use to:
 

Bring up a prompt before the rm command executes so that you get asked "are you sure?" when you use the command. Have the system display everything it is doing in great detail when it executes the rm command (verbosely, you might say).

Finishing Up
Take a bow, because you have successfully done all of the following using only the command line in Linux:
   

Listed the contents of, made, and removed directories Changed from working in one directory to another Copied, moved, renamed, and removed files Used the manual (man command) to get help on using commands

You might want to buy yourself a handy stuffed Tux for your desk at this point so that your friends will know you're learning Linux!

Additional Examples and Exercises
For additional practice, copy the musicians.txt file and use it to create a hierarchy similar to the following. Note that there is an error in the structure that you will need to correct before this will work.
more_cowbell >> more_more_cowbell >> >> more_musicians.txt >> >> even_more_musicians.txt >> more_more_cowbell >> >> additional_musicians.txt

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 2.5 License. ©2011 Google - Code Home - Site Terms of Service - Privacy Policy - Site Directory Google Code offered in: English - Español - 日本語 - 한국어 - Português - Pусский - 中文 (简体) - 中文(繁體)

Sponsor Documents

Or use your account on DocShare.tips

Hide

Forgot your password?

Or register your new account on DocShare.tips

Hide

Lost your password? Please enter your email address. You will receive a link to create a new password.

Back to log-in

Close