Basic Linux Commands

9
Basic Linux Commands Command Example Description cat Sends file contents to standard output. This is a way to list the contents of short files to the screen. It works well with piping. cat .bashrc Sends the contents of the ".bashrc" file to the screen. cd Change directory cd /home Change the current working directory to /home. The '/' indicates relative to root, and no matter what directory you are in when you execute this command, the directory will be changed to "/home". cd httpd Change the current working directory to httpd, relative to the current location which is "/home". The full path of the new working directory is "/home/httpd". cd .. Move to the parent directory of the current directory. This command will make the current working directory "/home. cd ~ Move to the user's home directory which is "/home/username". The '~' indicates the users home directory. cp Copy files cp myfile yourfile Copy the files "myfile" to the file "yourfile" in the current working directory. This command will create the file "yourfile" if it doesn't exist. It will normally overwrite it without warning if it exists. cp -i myfile yourfile With the "-i" option, if the file "yourfile" exists, you will be prompted before it is overwritten. cp -i /data/myfile . Copy the file "/data/myfile" to the current working directory and name it

description

Open Source Operating System

Transcript of Basic Linux Commands

Basic Linux CommandsCommand Example Descriptioncat Sends file contents to standard output. This is a way to list the

contents of short files to the screen. It works well with piping.cat .bashrc Sends the contents of the ".bashrc" file to the screen.

cd Change directorycd /home Change the current working directory to /home. The '/'

indicates relative to root, and no matter what directory you are in when you execute this command, the directory will be changed to "/home".

cd httpd Change the current working directory to httpd, relative to the current location which is "/home". The full path of the new working directory is "/home/httpd".

cd .. Move to the parent directory of the current directory. This command will make the current working directory "/home.

cd ~ Move to the user's home directory which is "/home/username". The '~' indicates the users home directory.

cp Copy filescp myfile yourfile Copy the files "myfile" to the file "yourfile" in the current

working directory. This command will create the file "yourfile" if it doesn't exist. It will normally overwrite it without warning if it exists.

cp -i myfile yourfile With the "-i" option, if the file "yourfile" exists, you will be prompted before it is overwritten.

cp -i /data/myfile . Copy the file "/data/myfile" to the current working directory and name it "myfile". Prompt before overwriting the file.

cp -dpr srcdir destdir Copy all files from the directory "srcdir" to the directory "destdir" preserving links (-p option), file attributes (-p option), and copy recursively (-r option). With these options, a directory and all it contents can be copied to another directory.

dddd if=/dev/hdb1 of=/backup/

Disk duplicate. The man page says this command is to "Convert and copy a file", but although used by more advanced users, it can be a very handy command. The "if" means input file, "of" means output file.

df Show the amount of disk space used on each mounted filesystem.

lessless textfile

Similar to the more command, but the user can page up and down through the file. The example displays the contents of textfile.

ln Creates a symbolic link to a file.ln -s test symlink Creates a symbolic link named symlink that points to the file

test Typing "ls -i test symlink" will show the two files are

different with different inodes. Typing "ls -l test symlink" will show that symlink points to the file test.

locate A fast database driven file locator.slocate -u This command builds the slocate database. It will take several

minutes to complete this command. This command must be used before searching for files, however cron runs this command periodically on most systems.

locate whereis Lists all files whose names contain the string "whereis".logout Logs the current user off the system.ls List files

ls List files in the current working directory except those starting with . and only show the file name.

ls -al List all files in the current working directory in long listing format showing permissions, ownership, size, and time and date stamp

more Allows file contents or piped output to be sent to the screen one page at a time.

more /etc/profile Lists the contents of the "/etc/profile" file to the screen one page at a time.

ls -al |more Performs a directory listing of all files and pipes the output of the listing through more. If the directory listing is longer than a page, it will be listed one page at a time.

mv Move or rename filesmv -i myfile yourfile Move the file from "myfile" to "yourfile". This effectively

changes the name of "myfile" to "yourfile".mv -i /data/myfile . Move the file from "myfile" from the directory "/data" to the

current working directory.pwd Show the name of the current working directory

more /etc/profile Lists the contents of the "/etc/profile" file to the screen one page at a time.

shutdown Shuts the system down.shutdown -h now Shuts the system down to halt immediately.shutdown -r now Shuts the system down immediately and the system reboots.

whereis Show where the binary, source and manual page files are for a command

whereis ls Locates binaries and manual pages for the ls command.

LINUX CLASSES - LINUX COMMANDS

How Do I Switch Users Under Linux?

Even if you have no schizophrenic tendencies, sometimes you'll want to become someone else while using Linux. For example, if you're logged in as hermie and you need to do something quickly that requires superuser authority, just enter the command su - root

In response to the su (switch user) command, you'll be prompted for the root account password. If you enter the password correctly, your prompt will change from a dollar sign to a pound sign (to reflect your status as root), and you will assume the powers of the root user. Issue the command

exit

to return to your previous identity. You can also use su to become any user on the system, not just root. For example, to become sigmund, you would enter this command:

su - sigmund

Don't forget the minus sign when you use su to temporarily become another user. Without it, the login profilefor that user is not executed--so it's not really the same as logging in, because your environment variables, and aliaseswould not change.

This would be like starting DOS without running the autoexec.bat file--things wouldn't work the same, because your personal setup commands (PATH and so on) would not run.

But why would you want to use su when you can have multiple log-ins via virtual consoles (see "Living in a Shell")? Because it's sometimes quicker or more convenient to switch between users using su, and because you may have no virtual consoles available--you may be using all of them or, if you're logged in to the machine via a modem, virtual consoles may not be available to you.

su-Linux Commandsu command- Substitute user identitySyntax: su [options]... [user [arg]...]

Description: The su Substitute User command allows you to become other existing users on the system. For example you can temporarily become root and execute commands as the super-user root. If you don't want anyone to su to root or restrict su command to certain users then add the following two lines to the top of your su configuration file in the /etc/pam.d/ directory. We highly recommend that you limit the person allowed to su to the root account.

Example:

Related: chroot, id, logname

usermod-Linux Command

Syntax: usermod [options] [user]

Description: Modify a user account. Changes may be made to the password, group membership, expiration date, and other attributes of a given user's account. With this command, a user's password may be locked, which has the effect of disabling the account.

Example:

Related: useradd, userdel

echo-Linux Commandecho command- Display message on screen

Syntax: echo [options]... [string]...

Description: Bash has the echo commands to provide comments for users, and although you should be familiar with at least the use of echo by now, we will discuss some more examples in the next sections.The echo built-in command outputs its arguments, separated by spaces and terminated with a newline character. The return status is always zero. echo takes a couple of options:-e: interprets backslash-escaped characters.-n: suppresses the trailing newline.

Example:

ps-Linux Commandps command- Process status, information about processes running in memory

Syntax:: Display Process infops option(s)

List all the keyword optionsps [-L]

Description: The ps will provide you a list of processes currently running. There is a wide variety of options that this command gives you.

Example:

Related: top, pstree, proc

df-Linux Commanddf command- Display free disk space

Syntax: df -options /dev/hdx

Description: The df is the simplest tool available to view disk usage. Simply type in df and you'll be shown disk usage for all your mounted filesystems in 1K blocks

Example:

Related:

2. diff command- Display the differences between two files

Syntax: diff file-1 file-2

who-Linux Commandwho command- Print who is currently logged in

Syntax: who [options] [file] [am i]

Description: Even easier than using the who and ps -u commands is to use the w. w will print out not only who is on the system, but also the commands they are running.

Example:

Related: groups, hostname, id, uname, logname, users, whoami

ls-Linux Commandls command: List information about FILEs, by default the current directory.Syntax: ls [Options]... [File]...

Description: The ls command, which lists files, is one of the most essential utilities for Unix and Linux users and, not surprisingly, one of the oldest. In its earliest form it was called listf and was available on the Massachusetts Institute of Technology's Compatible Time Sharing System (CTSS) by July, 1961. By 1963, there were a few options that could be used to vary what listf would list:

listflist files, newest first

listf rev list files, oldest firstlistf file extension give information about the named filelistf month day year list files older than the given date

In 1965, listf was extended to recognize ``*'' as a way to list all files that matched a specific pattern, with further improvements to the pattern matching in an updated version dated January 3, 1966. The 1966 version also generalized the syntax and added lots of options, including:

listf (file)list only files, not links

listf (auth) user list files created by the given userlistf (made) mmddyy [mmddyy] list files created between the given dateslistf (srec) list by sizelistf (smad) list by date of last modificationlistf (rev) list in reverse orderlistf (long) list in long format

When CTSS was superseded by Multics, the listf command was renamed to list, which could optionally be abbreviated to ls. The early version of ls had fewer options than late versions of listf had, but still included, along with a few others:

list -all(ls -a) list everything

list -date_time_modified (ls -dtm) list by date of last modificationlist -reverse (ls -rv) list in reverse order

When Bell Labs dropped out of Multics development in 1969 and work began on Unix, only the abbreviated name of list, ls, was retained. The First Edition (November 3, 1971) Unix manual documented the following options for ls, all of which are still available today:

ls -llist in long format

ls -t sort by time modifiedls -a list everything, including names starting with `.'ls -s include the size of files in the listingls -d list directories' names, not their contents

By the Fifth Edition (manual page dated March 20, 1974) the list of options for ls had expanded to include:

ls -rlist in reverse order

ls -u use access time, not modification timels -i list i-number for each filels -f don't sort the listing at all

The Sixth Edition (May, 1975) added one more:

ls -glist group IDs in the long format listing

In May and August, 1977, Bill Joy made some modifications of his own to ls at the University of California, Berkeley, which he subsequently distributed as part of the First Berkeley Software Distribution, 1BSD. The most dramatic difference with this version of ls was that it listed files in multiple columns rather than only listing one name per line. The options to control this new format were:

ls -1list one name per line (no columns)

ls -c list in columnsls -x list in columns, but sort across, not downls -q show control characters as `?'ls -m everything on one line, separated by commas

There was some controversy over whether it was appropriate to include code to print in columns as an integral part of ls or whether instead the formatting into columns should be done by a separate program into which the output of ls (or any other program) could be piped. At the beginning of 1979, Bell Labs released Seventh Edition Unix. Its version of ls did not incorporate the controversial changes, and had one new option that conflicted with a letter that Joy had also used:

use inode change time, not modification time

ls -c

A new Berkeley version of ls, dated August 26, 1980 and released with 4.0BSD, resolved the conflict by capitalizing the option to list in columns: ls -C. It also added to what the manual was by this time calling ``an unbelievable number of options:''

ls -Fmark directories with `/', programs with `*'

ls -R recursively list subdirectoriesls -b show control characters as their ASCII codes

Another revision in 4.2BSD (July 28, 1983) removed the -m, -b, and -x options -- but not before System V Release 2 had added all three of these to its own repertoire of options. They temporarily stayed alive there, but none of the three survived POSIX standardization so the 4.2BSD version of ls is essentially ls as we know it today. Example:

Related: dir, dircolors, dirname, quota, rm, rmdir, wc

man-Linux Commandman command- Display helpful information about commands.

Syntax: man [-k] [command]

info [command]

help [-s] [command]

Description: These commands access the manual and information pages on system commands and installed utilities. When available, the info pages usually contain more detailed descriptions than do the man pages.

Example:

Related: more ,less

echo-Linux Commandecho command- Display message on screen

Syntax: echo [options]... [string]...

Description: Bash has the echo commands to provide comments for users, and although you should be familiar with at least the use of echo by now, we will discuss some more examples in the next sections.The echo built-in command outputs its arguments, separated by spaces and terminated with a newline character. The return status is always zero. echo takes a couple of options:

-e: interprets backslash-escaped characters.-n: suppresses the trailing newline.

Example: