Home > Articles > Operating Systems, Server > Linux/UNIX/Open Source

Linux Basics

  • Print
  • + Share This

The Unix Command

Now we are ready to explore our environment by issuing commands to the shell. Unix has thousands of commands, which you can string together to make more complex commands. You can also create new commands in Unix. All this flexibility makes Unix an extremely powerful operating system.

The reason you have all this power is that commands are treated like any other executable file. When you type a command such as ls, you run the ls executable file, which is located in /bin/ls. When you create a new command, you simply add it to the /bin directory, then use it like any other Unix command. (If that doesn't make sense, you will understand by the end of the hour.)

Unix commands come with switches and options, which can extend or slightly change the action that the command performs. Switches are composed of a dash followed by a letter or series of letters that alter or enhance the command's output. Options consist of two dashes, followed by a word, that perform the same way as switches. In many cases, there is a switch and an option to perform the same task. A switch looks like this:

$ ls -a

An option looks like this:

$ ls --all

Some Basic Unix Commands

After you have logged in for the first time (using your user account, not root!), you should get to know your surroundings. Directories in Linux are based on a tree structure, just like in DOS and Windows. The very base of the directory tree is called the root directory. Below the root directory are more directories, called subdirectories, and below the subdirectories are the files.


Directories and subdirectories are the same thing as folders in Windows.


Getting Your Bearings with the pwd Command

If you lose track of the directory you're in, you can type the pwd command to show you the full pathname of your current location in the filesystem:

$ pwd
/home/judith

pwd stands for present working directory. pwd prints the full pathname of your current directory, starting from the root directory and ending at your current directory.


Notice that the slashes that separate directories in Unix are forward slashes (/), not backward slashes (\). Backward slashes also have a meaning in Unix, which we will discuss later.


The root directory is the very lowest level in the directory tree. You might remember the root directory from Hour 2, when you had to assign a mount point to /. Every other directory in your filesystem is a subdirectory of /.

If you enter pwd immediately after logging in, you should see something like /home/ username print to the screen (where username is your user name). When you first log in to Linux, you enter the filesystem at your home directory. Your home directory is where you keep all your work, as well as your personal configuration and other system files.

Listing Directory Contents with the ls Command

So now you know where you are. The next step is to find out what's in your home directory. To list the contents of any directory in Linux, type the following command:

$ls

If you're a new user, there probably won't be anything in your directory, unless your system administrator added files. There actually are files in your directory; you just need to add a switch to the ls command to see them:

$ls -a
. .. .bash_logout .bash_profile .bashrc .emacs .screenrc

The -a switch to the ls command means "all." That means to show all the files, including the hidden files that are displayed above. Hidden files are usually configuration files that are used by programs, but anyone can make a hidden file. Notice that no information is given about the files. The ls -a command doesn't tell you if an item is a file or directory, who owns it, who has permission to use it, when it was created, or anything. To get more detailed information about the contents of a directory, you need yet another switch:

$ ls -la
total 28
drwx------  2 judith  users    4096 Jul 18 14:29 .
drwxr-xr-x  10 root   root     4096 Jul 18 14:29 ..
-rw-r--r--  1 judith  users     24 Jul 18 14:29 .bash_logout
-rw-r--r--  1 judith  users     230 Jul 18 14:29 .bash_profile
-rw-r--r--  1 judith  users     124 Jul 18 14:29 .bashrc
-rw-rw-r--  1 judith  users     688 Jul 18 14:29 .emacs
-rw-r--r--  1 judith  users    3394 Jul 18 14:29 .screenrc

First, ls -la tells you the total size of your directory in blocks (1024 bytes) on the hard disk, which is 28 in our example. Each file and directory is listed with a lot of information.

Notice the first two entries in the ls -la output after the total size are simply a dot and a double-dot. If you are familiar with DOS, you have probably seen these before. The . stands for the current directory. The .. stands for the next-highest directory closer to root. Notice that both the . and the .. directories have a d at the beginning of the first string in the output. The d stands for directory. On some systems, that might be your only way of determining whether an item in a directory listing is a file or a subdirectory.


In Unix, commands are space and case-sensitive. If you forget the space between the command and the argument, or if you capitalize a file that should be lowercase, the command will not work the way you intended. For example, if you enter cd.., you will get an error message.



If you have a color monitor, directories appear in blue in the output of an ls command on a default Red Hat 7.0 system. This behavior is arbitrary, however, and is easily changed.


The letters and dashes that come after the d list the permissions for the directory (and for the files, as well). Permissions specify who has access to the file, as well as what kind of access each kind of user has. The name judith means that the user judith owns the file or directory (notice that root owns the .. directory, which is /home). The date and time, July 18, 14:29, is the date and time that the file was last modified. Finally, the name of the file appears at the far right.

Since you haven't created any files yet, your home directory is kind of boring. Let's use another command to explore the system a bit further.

Changing Directories with the cd Command

The command to change directories is cd. At the shell prompt, type cd followed by the pathname of the directory where you want to go. For example, if you type

$ cd /

you will go to the root directory. If we type ls in this new directory, we see a new set of directories:

$ cd /
bin  dev home lost+found opt  root tmp var
boot etc lib  mnt     proc sbin usr

If we type pwd, we see:

$ pwd
/

That way, we know for sure that we are in the root directory. The symbol for the root directory is /. No matter where you are in the directory tree, you can always return to the root directory by typing cd / and then pressing Enter.

Copying Files with the cp Command

Now we are at the second highest level in the directory tree. You will learn about all these directories in Hour 4, but for now let's just explore a bit to help you learn some more commands. Enter the following commands:

$cd /usr/X11R6/lib
$ls

The output of the ls command is shown in Figure 3.4. We are going to copy the XF86Config.eg file from this directory to our home directory.

Figure 3.4
The /usr/X11R6/lib/ X11 contains files needed to run X. We're just using a convenient sample configuration file from this directory as an example.


If you are not the administrator of your Linux system, you might not have access to any directories besides your home directory. If you don't have access, ask your system administrator for some sample files to play with.


The command to copy a file in Linux is cp. To copy a file, enter cp filename desired_location. For example:

$ cp XF86Config.eg /home/judith/

This tells the system to copy the file XF86Config.eg from the current directory to the /home/judith directory.

You don't have to be in a directory to copy a file from it. We could have stayed in /home/judith and written the following command instead:

$ cp /usr/X11R6/lib/X11/XF86Config.eg /home/judith/

or simply:

$ cp /usr/X11R6/lib/X11/XF86Config.eg .

As you may recall, the . at the end of the last command stands for "the current directory."

Now let's see what that file we just copied looks like. To return to our home directory from anywhere in the directory tree, we can simply type a tilde (~) at the shell prompt. Type cd ~, then pwd and see what happens:

$ cd ~
$pwd
/home/judith

Now type ls and verify that the file is there:

$ls
XF86Config.eg

Reading Output with the cat and more Commands and Using Ctrl+C

To view the contents of the file, we can use the cat command. Cat is short for concatenate, because you can also use it to combine the contents of two or more files.

$cat XF86Config.eg

This is a big file! This file is so big that it streams by too fast for you to read it. To slow down the cat command so that you can read one screenful of a long file at a time, you can pipe the cat command to the more command:

$cat XF86Config.eg | more

When you pipe a command in Unix, that means to execute one command, then apply the results of the first command to the second command. In this way, you can build some pretty powerful commands using smaller commands strung, or piped, together. The more command simply means display the output one screenful at a time. To scroll to the next screenful of text, press the spacebar. Continue pressing the spacebar until you reach the end of the file.

If you get tired of reading this very long file before you reach the end, you can issue the abort command, Ctrl+C.


Ctrl+C means to press Ctrl, then press C while holding down Ctrl. In this particular case, the shell isn't case sensitive, so pressing Ctrl+c is the same as pressing Ctrl+C.


Ctrl+C stops the currently running command and displays a shell prompt, ready for the next command.

Deleting Files with the rm Command

After you have finished using the XF86Config.eg file, you should delete it from your home directory, since it doesn't really belong there. To remove a file or directory, use the rm command:

$ rm /home/judith/XF86Config.eg
remove file 'XF86Config.eg'? y/n
y

Red Hat Linux gives you a prompt to make sure that you really want to delete the file. Other Unices and Linux distributions might simply delete the file with no warning.


There is no Undo button in Unix! Once you have deleted a file, it's gone.


The commands you have learned so far are shown in Table 3.1, along with a few new commands that you can experiment with on your own.

Table 3.1 A (Very) Few Common Unix Commands

Command

Description

pwd

Displays the present working directory

ls

Lists the contents of a directory

cd directory

Changes to directory

cp file new_location

Copies file to a new location or filename

cat file

Views file

more

Displays the output one screenful at a time

Ctrl+C

Aborts the command (only works while the command is still running)

rm file

Removes (deletes) file

mv file new_location

Moves a file to a new location, also used to rename a file

mkdir dir_name

Creates a new directory

rmdir dir_name

Removes an empty directory

rm -r dir_name

Removes a directory and all its contents



You can display the last few commands you entered by repeatedly pressing the up and down arrows. This way, you can avoid typing in long commands or filenames over again. You can edit the command on the command line, then press Enter to issue it again.


  • + Share This
  • 🔖 Save To Your Account

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020