Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

How do I change the password in Linux?

Answer:

From the command line use the passwd command. After typing passwd, you'll be
asked for the new password you wish to use and then asked to re-verify. Once you've
been returned back to the command line, your password has been successfully
changed as shown below.

passwd
Changing password for user root.
New UNIX password:
Retype new UNIX password:
passwd: all authentication tokens updated successfully.

How to copy a directory / folder.

For Linux / Unix users

To copy a directory with all subdirectories and files use the Linux / Unix cp
command. Below is an example command of how you would use the cp command
to copy files. Additional information about this command and other examples can
also be found in the above cp link.

cp -r /home/hope/files/* /home/hope/backup

In the above example the cp command would copy all files, directories, and
subdirectories in the /home/hope/files directory to the /home/hope/backup
directory.

How do I remove a full directory in Linux?

Answer:

To remove a directory that is full with other files and/or other directories, use the
below command.

rm -r directory

Note: In the above example, you would replace "directory" with the name of the full
directory you wish to delete. So if the directory was named files, you would type: rm
-r files

How can I renew or release an IP in Linux?

Answer:
Coming from a Microsoft operating system to Linux you may be surprised to see there
is not an option for ifconfig to release and renew an IP address. Below are two
different methods of how this can be done at the command line.

ifconfig eth0 down

Running the above command would take the eth0 interface (the first network
card) down. Which is the same as releasing the IP address from that network
card.

ifconfig eth0 up

After the interface is taken down, typing in the above command would bring that
interface back up.

or

dhclient eth0

Renews the IP address assigned to it by DHCP.

How do I change the name of a file or folder?

Linux and Unix users can rename their files and directories by using the mv command.
Below are some additional examples of how this command can be used. Additional
information about this command can also be found on the above command link.

Renaming a file

In the below example this would rename the file test.txt to hope.txt.

mv test.txt hope.txt

If the test.txt file was in a different directory then the one you were currently in
you would need to specify the path of the file. For example, if the file was in the
"computer" directory you would type a command similar to the below example.

mv computer/test.txt hope.txt

Renaming multiple files or directories at once

To rename multiple files at once you must utilize some form of wild character,
below are some examples of how this could be done.

In the below example this would rename all the files in the current directory that
end with .rtf to .txt files.
mv *.rtf *.txt

In this next example the command would rename a file with an unknown
character in the file name to something that can be read. The "?" used in the
below example is the wild character for an unknown character.

mv h?pe.txt hope.txt

Renaming a directory

Renaming a directory in Linux / Unix is much like renaming a file simply replace
the file name with the directory name that you wish to rename. For example, if
we wanted to rename the directory "test" to "hope" you would type the below
command.

mv test hope

How do I print a listing of files in a directory?

1. Navigate to the directory you wish to print the contents of. If you're new to
Linux, you will need to familiarize yourself with the Linux cd command and the
ls command.
2. Once in the directory you wish to print the contents of, type one of the below
commands.

ls > print.txt

Print each of the files and directories in the current directory to the print.txt
file.

How do I determine the size of a file?

Below are some of the different methods a *nix user can use to determine a size
of a file on their computer.

1. Move the the directory of the file you wish to view the size of.
2. Once in the directory, perform one of the below commands.

ls -l help.html

Performing the above command would list output similar to the below
information.

-rw-r----- 1 comphope www 11567230 Nov 24 01:12 log.txt

In the above output example, the 11567230 is the size of the file. For a more
user friendly output, use the du command as shown below.

du -h log.txt

This command would display the output "12M log.txt"

If you wish to see the total size of multiple files, type a command similar to
the below command.

du -ch *.txt

In the above example, the command would list every .txt file in the current
directory, display the size of each of those files as it was listing the files,
and the total of all the files combined.

How can I see how many files or directories are in a Linux directory?

Linux has several different methods of determining how many files and/or directories
are in a Linux directory. Below we have listed a few different methods of how you can
display the number of files and/or directories in Linux.

Use the tree command

The tree command is an easy and quick way to not only get a list of the files and
directories, but to get a report with how many files and directories there are. Keep in
mind if tree is run without any additional options that it will not only list the amount
of files and directories in the current directory, but all subdirectories as well.

If you wish to list only the contents of the current directory, you can use the below
command.

tree -i -L 1

See our tree command page for additional information about this command as well as
other available options.

Use echo in combination with wc

Users can also quickly see how many files and/or folders are in a directory by using
the echo command in combination with the wc command. Below are few different
examples of how this can be done.

Get a count of all files and directories in the current directory

echo * | wc
Once the above command has been typed you will get an output similar to the
below example. In this example the "10" indicates the amount of directories and
files in the current directory.

1 10 104

Get a count of only the directories in the current directory

echo */ | wc

Get a count of just the files in a directory

Note: This example is assuming all the files in the directory have extensions. If a
file does not have an extension it would not be counted.

echo *.* | wc

How do I rm or mv a file that begins with a dash?

Files that begin with a hyphen (dash) can be tricky to remove because Linux or Unix
will interpret the character after the dash as an option. For example, if the user
attempts to type the below command they would receive the error also shown below.

mv -myfile.txt myfile.txt
mv: invalid option -- m
Try `mv --help' for more information.

In the above example the operating system is interpreting the -m as a switch. For this
command to work you must type the below command.

mv ./-myfile.txt myfile.txt

Typing this command will rename the file in the current directory and will cause Linux
or Unix to not notice the dash.

How do I determine how much disk space I have in Linux / Unix?

Linux / Unix and their variants have several different ways of determining the
available free space and the capacity of a file, drive, a mount, or other share. Below
is a listing of commands that can be used to determine this information.

 df
 du
 ls
 vmstat
df
Report how much free disk space is available for each mount you have.

Syntax

df [OPTION]... [FILE]...

-a, --all include dummy file systems


-B, --block-size=SIZE use SIZE-byte blocks
-h, --human-readable print sizes in human readable format (e.g., 1K 234M 2G)
-H, --si likewise, but use powers of 1000 not 1024
-i, --inodes list inode information instead of block usage
-k like --block-size=1K
-l, --local limit listing to local file systems
--no-sync do not invoke sync before getting usage info (default)
-P, --portability use the POSIX output format
--sync invoke sync before getting usage info
-t, --type=TYPE limit listing to file systems of type TYPE
-T, --print-type print file system type
-x, --exclude-type=TYPE limit listing to file systems not of type TYPE
--version output version information and exit

Examples

df

In the above example when performing just the df command with no additional
switches or specification of the file or directory you would get a listing of all file
systems and their used and available space.

df -h

The above command is one of the most commonly used commands as it displays
the sizes in an easy to read format as shown in the below example.
Filesystem Size Used Avail Use% Mounted on
/dev/hda2 28G 7.6G 19G 29% /
tmpfs 252M 0 252M 0% /lib/init/rw
tmpfs 252M 0 252M 0% /dev/shm
/dev/hda1 464M 37M 403M 9% /boot
/dev/hda3 8.3G 429M 7.5G 6% /var
nfs6:/home 520G 461G 60G 89% /home
mirrors:/mirrors 1.4T 1.1T 233G 83% /mirrors
/dev/mapper/big-phat 77G 36G 42G 46% /space
mailstore:/var/mail 9.9G 4.8G 4.7G 51% /var/mail

df -b public_html

In the above example this command would display the amount of free space in
the public_html directory. Below is an example of the output may display when
performing this command.

Filesystem avail
nfs.computerhope.com:/home 10068252

du

Linux / Unix du command


Quick links

About du
Syntax
Examples
Related commands
Linux / Unix main page

About du

Tells you how much space a file occupies.


Syntax

du [-a] [-k] [-s] [-d] [-L] [-o] [-r] [-x] directories

-a Displays the space that each file is taking up.

-k Write the files sizes in units of 1024 bytes, rather than the default 512-byte
units.

-s Instead of the default output, report only the total sum for each of the
specified files.

-d Do not cross filesystem boundaries. For example, du -d / reports usage only


on the root partition.

-L Process symbolic links by using the file or directory which the symbolic link
references, rather than the link itself.

-o Do not add child directories' usage to a parent's total. Without this option,
the usage listed for a particular directory is the space taken by the files in
that directory, as well as the files in all directories beneath it. This option
does nothing if -s is used.

-r Generate messages about directories that cannot be read, files that cannot
be opened, and so forth, rather than being silent (the default).

-x When evaluating file sizes, evaluate only those files that have the same
device as the file specified by the file operand.

directories Specifies the directory or directories.

Examples

du -s *.txt - Would report the size of each txt file in the current directory. Below
is an example of the output.

8 file1.txt
8 file2.txt
10 file3.txt
2 file4.txt
8 file5.txt
8 file6.txt

du -ch *.txt - Display the size of the txt files in a friendly size format listing as
well as the total capacity of all the files combined.

ls
Lists the contents of a directory.

Syntax

ls [-a] [-A] [-b] [-c] [-C] [-d] [-f] [-F] [-g] [-i] [-l] [-L] [-m] [-o] [-p] [-q] [-r] [-R] [-s] [-t]
[-u] [-x] [pathnames]

-a Shows you all files, even files that are hidden (these files begin with a
dot.)
-A List all files including the hidden files. However, does not display the
working directory (.) or the parent directory (..).
-b Force printing of non-printable characters to be in octal \ddd notation.
-c Use time of last modification of the i-node (file created, mode changed,
and so forth) for sorting (-t) or printing (-l or -n).
-C Multi-column output with entries sorted down the columns. Generally
this is the default option.
-d If an argument is a directory it only lists its name not its contents.
-f Force each argument to be interpreted as a directory and list the name
found in each slot. This option turns off -l, -t, -s, and -r, and turns on -
a; the order is the order in which entries appear in the directory.
-F Mark directories with a trailing slash (/), doors with a trailing greater-
than sign (>), executable files with a trailing asterisk (*), FIFOs with a
trailing vertical bar (|), symbolic links with a trailing at-sign (@), and
AF_Unix address family sockets with a trailing equals sign (=).
-g Same as -l except the owner is not printed.
-i For each file, print the i-node number in the first column of the report.
-l Shows you huge amounts of information (permissions, owners, size, and
when last modified.)
-L If an argument is a symbolic link, list the file or directory the link
references rather than the link itself.
-m Stream output format; files are listed across the page, separated by
commas.
-n The same as -l, except that the owner's UID and group's GID numbers are
printed, rather than the associated character strings.
-o The same as -l, except that the group is not printed.
-p Displays a slash ( / ) in front of all directories.
-q Force printing of non-printable characters in file names as the character
question mark (?).
-r Reverses the order of how the files are displayed.
-R Includes the contents of subdirectories.
-s Give size in blocks, including indirect blocks, for each entry.
-t Shows you the files in modification time.
-u Use time of last access instead of last modification for sorting (with the
-t option) or printing (with the -l option).
-x Displays files in columns.
-1 Print one entry per line of output.
pathnames File or directory to list.

Examples

ls -l

In the above example this command would list each of the files in the current
directory and the files permissions, the size of the file, date of the last
modification, and the file name or directory. Below is additional information
about each of the fields this command lists.

Permissions Directories Group Size Date Directory or file


drwx------ 2 users 4096 Nov 2 19:51 mail/
drwxr-s--- 35 www 32768 Jan 20 22:39 public_html/
-rw------- 1 users 3 Nov 25 02:58 test.txt

Below is a brief description of each of the above categories shown when using the ls -l
command.

Permissions - The permissions of the directory or file.


Directories - The amount of links or directories within the directory. The default
amount of directories is going to always be 2 because of the . and .. directories.

Group - The group assigned to the file or directory

Size - Size of the file or directory.

Date - Date of last modification.

Directory of file - The name of the file or file.

ls ~

List the contents of your home directory by adding a tilde after the ls command.

ls /

List the contents of your root directory.

ls ../

List the contents of the parent directory.

ls */

List the contents of all sub directories.

ls -d */

Only list the directories in the current directory.

vmstat
Display information about the kernel threads, virtual memory, disks, traps, and CPU
activity.

Syntax

vmstat [-V] [-n] [delay [count]]

-V Print version information.


-n causes the headers not to be reprinted regularly.
-a print inactive/active page stats.
-d prints disk statistics
-D prints disk table
-p prints disk partition statistics
-s prints vm table
-m prints slabinfo
-S unit size
unit size k:1000 K:1024 m:1000000 M:1048576 (default is K)
delay delay between updates in seconds.
count number of updates.

Examples

vmstat

Running the vmstat command with no extra options would display information similar
to the below information.

procs -----------memory---------- --- -----io- --system- ----cpu-


swap-- --- - ---
r b swpd free buff cache si so bi bo in cs sy id wa
0 0 7412 18872 0 0 2 7 us 1 96 0
18044 110132 26 22
18044 110132 2

How do I know what kernel / distro of Linux I have?


Answer:

Using the uname command, you can quickly and easily identify what kernel version
you're using. as shown below.

uname -arv
SunOS hope 5.7 Generic_106541-08 sun4m sparc SUNW,SPARCstation-10

uname -r
2.6.24-19-generic
Using the cat command to examine the contents of specific files can also help identify
more information about the system. Below is a list of these files. Unfortunately, not
all distros of Linux are the same. Therefore, this list displays the Linux type and then
the cat file to edit.

CentOS Linux distro

cat /proc/version

Debian Linux distro

cat /etc/debian_version

Redhat Linux distro

cat /etc/redhat-release

Ubuntu Linux distro

cat /etc/issue

or

cat /etc/lsb-release

You might also like