20140210-1 选题

This commit is contained in:
DeadFire 2014-02-10 16:13:07 +08:00
parent df99c3224d
commit 28278c092e
4 changed files with 642 additions and 0 deletions

View File

@ -0,0 +1,285 @@
10 Linux/Unix Bash and KSH Shell Job Control Examples
================================================================================
![](http://s0.cyberciti.org/uploads/cms/2014/02/unix-llnux-shell-job-control-series.jpg)
Linux and Unix are multitasking operating systems i.e. a system that can run multiple tasks (process) during the same period of time. In this new blog series, I am going to list the Linux and Unix job control commands that you can use for multitasking with the Bash or Korn or POSIX shell.
### What is a job control? ###
Job control is nothing but the ability to stop/suspend the execution of processes (commands) and continue/resume their execution as per your requirements. This is done using your operating system and shell such as bash/ksh or POSIX shell.
### Who provides a facility to control jobs? ###
The Bash / Korn shell, or POSIX shell provides a facility to control jobs.
### Say hello to job table ###
Your shell keeps a table of current jobs, called job table. When you type command the shell assigns a jobID (also known as JOB_SPEC). A jobID or JOB_SPEC is nothing but small integer numbers.
#### #1: Creating your first Linux/Unix job ####
I am going to run a command called xeyes that displays two googly eyes on screen, enter:
$ xeyes &
Sample outputs:
[![](http://s0.cyberciti.org/uploads/cms/2014/02/run-xeyes-command-in-background.jpg)][4]
*Fig.01: Running the xeyes command in the background*
I started a job in the background with an ampersand (&). The shell prints a line that looks like the following:
[1] 6891
In this example, two numbers are output as follows
- [1] : The xeyes job, which was started in the background, was job number 1.
- 6891 : A process ID of job number 1.
I am going to start a few more jobs:
## Start a text editor, system load average display for X, and sleep command ##
gedit /tmp/hello.c &
xload &
sleep 100000 &
#### #2: List the current jobs ####
To [see the status of active jobs in the current shell][1], type:
$ jobs
$ jobs -l
Sample outputs:
[1] 9379 Running xeyes &
[2] 9380 Running gedit /tmp/hello.c &
[3]- 9420 Running xload &
[4]+ 9421 Running sleep 100000 &
A brief description of each field is given below:
(注:表格部分,这样发表出来应该会方便看一点)
<table border="1"><tbody><tr><th>Field</th><th>Value</th><th>Description</th><th>Example(s)</th></tr><tr><td>1</td><td>[1]</td><td><strong>jobID</strong> or <strong>JOB_SPEC</strong> - Job number to use with the fg, bg, wait, kill and other shell commands. You must prefix the job number with a percent sign (<kbd><strong>%</strong></kbd>).<br>A plus sign (<kbd>+</kbd>) identifies the default or current job.<br>A minus sign (<kbd>-</kbd>) identifies the previous job.</td><td><kbd>%1</kbd><br><kbd>fg %1</kbd><br><kbd>kill %2</kbd></td></tr><tr><td>2</td><td>9379</td><td>P<strong>rocess ID</strong> - An identification unique number that is automatically assigned to each process when it is created on the system.</td><td>kill 9379</td></tr><tr><td>3</td><td>Running</td><td><strong>state</strong> - The state of job:<br><kbd><strong>Running</strong></kbd> - The job is currently running and has not been suspended by a signal.<br><kbd><strong>Stopped</strong></kbd> - The job was suspended.<br></td><td>N/A</td></tr><tr><td>4</td><td>xeyes &amp;</td><td><strong>command</strong> - The command that was given to the shell.</td><td>script &amp;<br>firefox url&amp;</td></tr></tbody></table>
You can also use ps command to list the processes running on the system:
$ ps
#### #3: Stop or suspend running jobs ####
Hit the [Ctrl]-[Z] key or use [kill command][2] as follows:
kill -s stop PID
In this example, start [ping command][3] and use the Ctrl-Z key sequence to stop the ping command job:
[![](http://s0.cyberciti.org/uploads/cms/2014/02/suspend-unix-job-command.gif)][5]
*Animated gif 01: Suspending ping command job*
#### #4: Resume suspended/stopped job in the foreground ####
Let us [resume or bring stopped ping job to the foreground][6] and make it the current job with the help of [fg command][7]. The syntax is as follows:
## Job id number 5 for ping command ##
fg %5
I can also state any job whose command line begins with the string "ping":
## %String ##
fg %ping
Sample outputs:
64 bytes from www.cyberciti.biz (75.126.153.206): icmp_req=3 ttl=53 time=265 ms
64 bytes from www.cyberciti.biz (75.126.153.206): icmp_req=4 ttl=53 time=249 ms
64 bytes from www.cyberciti.biz (75.126.153.206): icmp_req=5 ttl=53 time=267 ms
^C
#### #5: Resume suspended/stopped job in the background ####
In this example, I am going to update all installed packages on Red Hat or CentOS Linux production server using [yum command][8] background job:
# yum -y update &>/root/patch.log &
However, due to some reason (say load issue) I decided to stop this job for 20 minutes:
# kill -s stop %yum
Sample outputs:
[7]+ Stopped yum -y update &>/root/patch.log &
#### Restart a stopped background yum process with bg ####
Now, I am going to [resume stopped the yum -y update &>/root/patch.log & job][9], type:
# bg %7
OR
# bg %yum
Sample outputs:
[7]+ yum -y update &>/root/patch.log &
#### #6: Kill a job / process ####
To stop/kill a [yum command][10] process, enter the following [kill command][11] whose jobID was 7:
# kill %7
OR
# kill pid
Sample outputs:
[7]+ Terminated yum -y update &>/root/patch.log &
On Linux/FreeBSD/OS X Unix you can [use killall command to kill process by name instead of PID][12] or jobID.
#### #7 Why does shell kill off all my background jobs when I logout? ####
In this example, I am going to start pdfwriter.py job to generate pdf files for [this site][13] in bulk:
~/scripts/www/pdfwriter.py --profile=faq --type=clean --header=logo\
--footer-left "nixCraft is GIT UL++++ W+++ C++++ M+ e+++ d-" \
--footer-right "Page [of] of [total]" &
As soon as I logout from shell, pdfwriter.py job will be killed by my shell. To overcome this problem use [disown shell builting command to tell the shell not to send a HUP signal][14], type:
$ ~/scripts/www/pdfwriter.py --profile=faq .... &
$ disown
$ exit
#### #8 Prevent job from being killed on logout using an external command called nohup ####
You can also use [nohup command to execute jobs after you exit from a shell prompt][15]:
$ nohup ~/scripts/www/pdfwriter.py --profile=faq .... &
$ exit
#### #9: Finding the PID of last job ####
To find the the process ID of the most recently executed background (asynchronous) command, use bash shell special parameter $!
$ gedit foo.txt &
$ echo "PID of most recently executed background job - $!"
Sample outputs:
PID of most recently executed background job - 9421
#### #10: Wait for job completion ####
The wait command waits for given process ID or jobID (job specification) , and reports its termination status. The syntax is as follows:
/path/to/large-job/command/foo &
wait $!
/path/to/next/job/that-is-dependents/on-foo-command/bar
Here is one of my working script:
#!/bin/bash
# A shell script wrapper to create pdf files for our blog/faq section
########################################################################
# init() - Must be run first
# Purpose - Create index file in $_tmp for all our wordpress databases
########################################################################
init(){
_php="/usr/bin/php"
_phpargs="-d apc.enabled=0"
_base="~/scripts"
_tmp="$_base/tmp"
_what="$1"
for i in $_what
do
[[ ! -d "$_tmp/$i" ]] && /bin/mkdir "$_tmp/$i"
$_php $_phpargs -f "$_base/php/rawsqlmaster${i}.php" > "$_tmp/$i/output.txt"
done
}
#####################################################
# Without index file, we can out generate pdf files
#####################################################
init blog
###########################################################
# Do not run the rest of the script until init() finished
###########################################################
wait $!
## Alright, create pdf files
~/scripts/www/pdfwriter.py --profile=blog --type=clean --header=logo\
--footer-left "nixCraft is GIT UL++++ W+++ C++++ M+ e+++ d-" \
--footer-right "Page [of] of [total]"
#### Linux and Unix job control command list summery ####
<table border="1"><tbody><tr><th>Command</th><th>Description</th><th>Example(s)</th></tr><tr><td><kbd><strong>&amp;</strong></kbd></td><td>Put the job in the background</td><td><kbd>command &amp;</kbd></td></tr><tr><td><kbd><strong>%n</strong></kbd></td><td>Set the job with the given n (number)</td><td><kbd>command %1</kbd></td></tr><tr><td><kbd><strong>%Word</strong></kbd></td><td>Refer the job whose command line begins with the Word</td><td><kbd>command %yum</kbd></td></tr><tr><td><kbd><strong>%?Word</strong></kbd></td><td>Refer any job whose command line contains the Word</td><td><kbd>command %?ping</kbd></td></tr><tr><td><kbd><strong>%%</strong></kbd><br><kbd><strong>%+</strong></kbd></td><td>Refer to the current job</td><td><kbd>kill %%<br>kill %+</kbd></td></tr><tr><td><kbd><strong>%-</strong></kbd></td><td>Refer to the previous job</td><td><kbd>bg %-</kbd></td></tr><tr><td><kbd><strong>CTRL-Z</strong><br><kbd><strong>kill -s stop jobID</strong></kbd></kbd></td><td>Suspend or stop the job</td><td><kbd>kill -s stop %ping</kbd></td></tr><tr><td><kbd><strong>jobs</strong><br><kbd><strong>jobs -l</strong></kbd></kbd></td><td>List the active jobs</td><td><kbd>jobs -l</kbd></td></tr><tr><td><kbd><strong>bg</strong></kbd></td><td>Put jobs to the background</td><td><kbd>bg %1<br>bg %ping</kbd></td></tr><tr><td><kbd><strong>fg</strong></kbd></td><td>Put job to the foreground</td><td><kbd>fg %2<br>fg %apt-get</kbd></td></tr></tbody></table>
#### A note about shell built-in and external commands ####
Run the following type command to find out whether given command is internal or external:
type -a fg bg jobs disown
Sample outputs:
fg is a shell builtin
fg is /usr/bin/fg
bg is a shell builtin
bg is /usr/bin/bg
jobs is a shell builtin
jobs is /usr/bin/jobs
disown is a shell builtin
In almost all cases, you need to use shell built-in commands. All external commands such as /usr/bin/fg or /usr/bin/jobs works in a different shell environment, and can not use parent shell's environment.
#### Conclusion ####
I hope you enjoyed this blog post series ([rss feed][16]) and I suggest that you read the following for more information:
- See our faq section for [disown command examples][17], [jobs command examples][18], [bg command examples][19], and [fg command examples][20] on Linux/Unix process management.
- Man pages [bash(1)][21], [ksh(1)][22], [ps(1)][23], [kill(1)][24]
- [Korn shell (ksh93) documentation][25].
- [NU bash shell documentation][26].
I am planning to add more in-depth tutorial in this series. Please let me know if you need to see specific topic in the comment section below.
--------------------------------------------------------------------------------
via: http://www.cyberciti.biz/howto/unix-linux-job-control-command-examples-for-bash-ksh-shell/
译者:[译者ID](https://github.com/译者ID) 校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](http://linux.cn/) 荣誉推出
[1]:http://www.cyberciti.biz/faq/unix-linux-jobs-command-examples-usage-syntax/
[2]:http://www.cyberciti.biz/faq/unix-kill-command-examples/
[3]:http://www.cyberciti.biz/faq/unix-ping-command-examples/
[4]:http://www.cyberciti.biz/howto/unix-linux-job-control-command-list-for-bash-ksh-shell/attachment/run-xeyes-command-in-background/
[5]:http://www.cyberciti.biz/howto/unix-linux-job-control-command-list-for-bash-ksh-shell/attachment/suspend-unix-job-command/
[6]:http://www.cyberciti.biz/faq/unix-linux-fg-command-examples-usage-syntax/
[7]:http://www.cyberciti.biz/faq/unix-linux-fg-command-examples-usage-syntax/
[8]:http://www.cyberciti.biz/faq/rhel-centos-fedora-linux-yum-command-howto/
[9]:http://www.cyberciti.biz/faq/unix-linux-bg-command-examples-usage-syntax/
[10]:http://www.cyberciti.biz/faq/rhel-centos-fedora-linux-yum-command-howto/
[11]:http://www.cyberciti.biz/faq/unix-kill-command-examples/
[12]:http://www.cyberciti.biz/faq/unix-linux-killall-command-examples-usage-syntax/
[13]:http://www.cyberciti.biz/faq/
[14]:http://www.cyberciti.biz/faq/unix-linux-disown-command-examples-usage-syntax/
[15]:http://www.cyberciti.biz/tips/nohup-execute-commands-after-you-exit-from-a-shell-prompt.html
[16]:http://www.cyberciti.biz/tutorials/practical-unixlinux-series/feed/
[17]:http://www.cyberciti.biz/faq/unix-linux-disown-command-examples-usage-syntax/
[18]:http://www.cyberciti.biz/faq/unix-linux-jobs-command-examples-usage-syntax/
[19]:http://www.cyberciti.biz/faq/unix-linux-bg-command-examples-usage-syntax/
[20]:http://www.cyberciti.biz/faq/unix-linux-fg-command-examples-usage-syntax/
[21]:http://www.manpager.com/linux/man1/bash.1.html
[22]:http://www.manpager.com/linux/man1/ksh.1.html
[23]:http://www.manpager.com/linux/man1/ps.1.html
[24]:http://www.manpager.com/linux/man1/kill.1.html
[25]:http://www2.research.att.com/sw/download/man/man1/ksh.html
[26]:https://www.gnu.org/software/bash/manual/bashref.html

View File

@ -0,0 +1,56 @@
How To Install Google Keep In Ubuntu 13.10 [Quick Tip]
================================================================================
![](http://itsfoss.com/wp-content/uploads/2014/02/Google-keep-logo.jpg)
Lately, Google Keep has been my favorite quick note taking app in Android. While working on Ubuntu, I often tend to take some quick note as well. Since Google Keep syncs between devices, it makes sense to **install Google Keep in Ubuntu** as well. In this quick tip, I am going to show you how to install Google Keep in Ubuntu 13.10 or other Linux distributions.
If you are a Google Chrome user, you might be aware of Google desktop apps. There are a bunch of apps in Chrome Web Store that can be found in “**For your desktop**” collection. Unlike regular Chrome apps that run inside the browser, these desktop apps run standalone, like a desktop app. Well see how can we leverage this new feature to install chrome apps as native Ubuntu apps.
### Install Google Keep in Ubuntu 13.10: ###
Though I am taking an example of Google Keep here, you can use the same procedure to install other apps such as [Any.Do][1] or [Pocket][2] (from For your desktop collection). Quite obviously **you must have Google Chrome web browser installed in your system**. It may or may not work with Chromium browser.
### Step 1: ###
Open a new tab in Google Chrome. Go to Chrome Web Store and install Google Keep.
### Step 2: ###
Now from the new tab, **right click** on the Google Keep icon and select **Create shortcuts**.
![](http://itsfoss.com/wp-content/uploads/2014/02/Install_Google_keep_Ubuntu.jpeg)
### Step 3: ###
Check both Desktop and Application menu.
![](http://itsfoss.com/wp-content/uploads/2014/02/Install_Google_Keep_Ubuntu_2.png)
That actually is it. Quite easy in fact. You can now find Google Keep in Unity Dash and lock it to launcher. Even if you are not running Chrome, you can simply run Google Keep as a normal Ubuntu app.
![](http://itsfoss.com/wp-content/uploads/2014/02/Install_Google_keep_Ubuntu_3.jpeg)
Cool, isnt it? Google is planning to port Google App Launcher to Linux as well. And soon we will also have Google Now desktop app for Linux which is currently available only in dev channel.
Thinking about all these, I wonder why Google has not worked on a native Google Drive client for Linux. Of course there are third party apps such as [SyncDrive to sync Google Drive like Dropbox in Ubuntu][3] but an official Google Drive would be much appreciated.
--------------------------------------------------------------------------------
via: http://itsfoss.com/install-google-keep-ubuntu-1310/
译者:[译者ID](https://github.com/译者ID) 校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](http://linux.cn/) 荣誉推出
[1]:http://www.any.do/
[2]:http://getpocket.com/
[3]:http://itsfoss.com/sync-multiple-google-drive-dropbox-syncdrive-ubuntu/
[4]:
[5]:
[6]:
[7]:
[8]:
[9]:
[10]:
[11]:
[12]:

View File

@ -0,0 +1,164 @@
Linux touch command - Why do we need to change Timestamp?
================================================================================
We may use touch command in Linux to create an empty file. Of course we can use another command such as vi, nano or any editing tools to do it. But you may need more steps do it. But do know it that the main function of touch command is not create an empty file?
### What is touch command ###
As we know, every file has timestamp attached to it. This time stamp is access time and modification time. Touch command is a command for file access and modification time.
### Why we need touch ###
Since touch command description is to change timestamp, we may wonder why do we need to change timestamp? This question also comes to our mind. But then, we think there is a reason why we need it. If you are participating in open source projects over sea, then timestamp may become important. Compiling a source code files may need a same region of timestamp. If not, the program may failed to compiled.
### Run touch without options ###
To use touch command is quite simple. Just type :
$ touch file_name
Please take a look a screenshot below.
![Touch command](http://linoxide.com/wp-content/uploads/2014/01/touch.png)
**File_1.txt** originally has timestamp 12:42. After we do touch command, it changed to 17:08. By default, touch will change the file timestamp into current time.
### Change only Access time ###
As we mention before that every file has Access time and Modify time attached to the file. File_1.txt above has timestamp 17:08. We can see more detail about it.
![Detail timestamp](http://linoxide.com/wp-content/uploads/2014/01/touch_stat.png)
We see that **Access** time and **Modify** time has the same value which is 17:08:35 and the timezone is GMT +7.
Now if we want to change only the Access time, we need to use **-a** option.
$ touch -a file_1.txt
![Touch -a option](http://linoxide.com/wp-content/uploads/2014/01/touch_a.png)
As you see, the **Access time is changed** into 17:51:37 but the **Change time is still** 17:08:35
### Change only Modify time ###
To do this, we can use **-m** option. Now we will use **file_2.txt** as an example.
![File_2.txt detail timestamp](http://linoxide.com/wp-content/uploads/2014/01/touch_stat_file_2.png)
$ touch -m file_2.txt
![Touch -m option](http://linoxide.com/wp-content/uploads/2014/01/touch_m.png)
Now the **Modify time is change from 12:42:20 to 17:57:20. Please note**, that the Change field value will always keep record when the file is accessed or modified.
### Change into custom timestamp ###
Both -**a and -m** option will change the file timestamp into current time. We can also possible to change it into custom timestamp. To do this, use **-t** option.
From file_2.txt above example, we knew that its time stamp are :
12:42:20 for Access time
17:57:20 for Modify time
2014-01-14 for the date
Let say we want to change it into **09:58:27** for **both Access time and Modify time and 12 January 2014**. To do this, we can use this command :
$ touch -t 201401120958.27 file_2.txt
![Touch using -t option](http://linoxide.com/wp-content/uploads/2014/01/touch_t.png)
**-t** option is consist of :
#### [[CC]YY]MMDDhhmm [.SS] ####
CC - The first two digits of the year
YY - The second two digits of the year
MM - The month of the year [01-12]
DD - The day of the month [01-31]
hh - The hour of the day [00-23]
mm - The minute of the hour [00-59]
SS - The second of the minute [00-61]
### Another way to change date and time ###
If you feel that the **[[CC]YY]MMDDhhmm [.SS]** format is not comfortable for you, we can use -d option. Heres an example how to use **-d** option.
#### Change the date to specific date ####
For example we have file named file_3.txt with attributes as shown in the picture below.
![File_3.txt detail timestamp](http://linoxide.com/wp-content/uploads/2014/01/stat_file_3.png)
Now **we want to change the date** from 14 January 2014 to 10 December 2013. We can use with this command :
$ touch -d 10-December-2013 file_3.txt
![Change date using -d option](http://linoxide.com/wp-content/uploads/2014/01/touch_d_date.png)
We see now that the date for Access and Modify entries is changed into 10 December 2013.
### Change the timezone ###
If we want to change it into time in specific GMT, we can also use with **-d** option. Lets return the file_3.txt into the current time.
$ touch file_3.txt
![Reset file_3.txt timestamp](http://linoxide.com/wp-content/uploads/2014/01/touch_return_file_3.png)
We can see that file_3.txt has GMT +0700 timezone. To change it into GMT3 timezone, we can use this command.
$ touch -d GMT3 file_3.txt
![Change into GMT3 timezone](http://linoxide.com/wp-content/uploads/2014/01/touch_gmt3.png)
Now the time is change into 10:00:00 AM
### Combining values using -d option ###
There is something cool using **-d** option. Please take a look at the picture below.
![Change date using keyword](http://linoxide.com/wp-content/uploads/2014/01/touch_next_sunday.png)
We can use the word **next Sunday** and combine it with GMT 3 values, and touch command can still recognize it. The date is changed into 21 January 2014, where the current date is 14 January 2014.
Heres another example of **-d** option.
First, we reset file_3.txt into current date and time.
$ touch file_3.txt
![Reset file_3.txt timestamp](http://linoxide.com/wp-content/uploads/2014/01/touch_reset_file_3.png)
Then we do this command :
$ touch -d 1 year ago 13:43:07 file_3.txt
![Change date using keyword](http://linoxide.com/wp-content/uploads/2014/01/touch_d_year_ago.png)
Touch even recognize a word **1 year ago**. The date and time is now changed into 14 January 2013 and 13:43:07 time.
### Create an empty file ###
When you run touch command and the destination file is not exist, then touch will create an empty file with the same name.
$ touch file_10.txt
![Create an empty file](http://linoxide.com/wp-content/uploads/2014/01/touch_file_10.png)
### Create multiple files at the same time ###
To create multiple files, you can put the file names separated by space.
$ touch doc_10.txt doc_20.txt doc_30.txt
![Create multiple files](http://linoxide.com/wp-content/uploads/2014/01/touch_multiple_files.png)
### Conclusion ###
Touch will useful for you if you are dealing with a timestamp of files and directories. As usual you can always type **man touch** or **touch --help** to display its manual page to explore more detail.
--------------------------------------------------------------------------------
via: http://linoxide.com/linux-command/linux-touch-command/
译者:[译者ID](https://github.com/译者ID) 校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](http://linux.cn/) 荣誉推出

View File

@ -0,0 +1,137 @@
Moving or Rename File / Directory in Linux - 10 Practical mv Command Examples
================================================================================
After knowing about [copy command][1], the next command which is related is mv command. When you want to move files from one place to another and you dont want to duplicate it, then **mv command** is absolutely right for this task.
### What is mv command ###
mv command is a command that similar with **cp**, but it **does not** create a copy / duplicate of files or directories. This command is installed by default on your Linux system, in any kind of Linux you are using. Please take a look of some examples using mv command in day-to-day operation.
### 1. Moving files ###
The requirement of moving file is the file source location **must be different** with the files destination location. Heres an example. To move **file_1.txt** from current directory to another directory , for example **/home/pungki/office**, heres the syntax :
$ mv file_1.txt /home/pungki/office
![mv command](http://linoxide.com/wp-content/uploads/2014/01/mv.png)
As we can see, when we move the file_1.txt, the file_1.txt from previous directory **is deleted**.
### 2. Moving multiple files ###
If we want to move multiple files, we put them in one line separated by space.
$ mv file_2.txt file_3.txt file_4.txt /home/pungki/office
![Move multiple files](http://linoxide.com/wp-content/uploads/2014/01/mv_multiple.png)
You can also use pattern if your files have it. For example, to move all files which have **.txt** extension, we can use this command :
$ mv *.txt /home/pungki/office
![Move using pattern](http://linoxide.com/wp-content/uploads/2014/01/mv_pattern.png)
### 3. Moving directory ###
Different from the copy command, moving directory using mv command is pretty straight forward. To move a directory, you can just to use mv command without any options. Please take a look screenshot below.
![Moving directory](http://linoxide.com/wp-content/uploads/2014/01/mv_directory.png)
### 4. Renaming files or directory ###
We also use mv command to rename files and directory. But in order to do so, the destination location **must be the same** with the source location. Then the file name must be different.
Let say we are inside **/home/pungki/Documents** folder and we want to **rename file_1.txt into file_2.txt**. Then the command will be like :
$ mv file_1.txt file_2.txt
If we mention the absolute path, then it will look like this :
$ mv /home/pungki/Documents/file_1.txt /home/pungki/Documents/file_2.txt
![Renaming file](http://linoxide.com/wp-content/uploads/2014/01/ren_file.png)
### 5. Renaming directory ###
The above rule is also applied to directory. Take a look this example :
$ mv directory_1/ directory_2/
![Renaming directory](http://linoxide.com/wp-content/uploads/2014/01/ren_directory.png)
### 6. Print what happen ###
When you are moving or renaming a large number of file / directory, you may want to know does your command works successfully or not without seeing to the destination location. To do this, we can use **-v** option.
For example we want to move all txt files and want to check it. Then the command will be like this.
$ mv -v *.txt /home/pungki/office
![mv with verbose mode](http://linoxide.com/wp-content/uploads/2014/01/mv_v.png)
The same way is applied to directory.
![mv directory with verbose mode](http://linoxide.com/wp-content/uploads/2014/01/mv_v_directory.png)
### 7. Using interactive mode ###
When you are moving file into another location, and there is already exist the same file, then by default mv will overwrite it. No pop-up notification for this. To make a notification for overwriting file, we can use **-i** option.
Let say we want to move file_1.txt to /home/pungki/office. Meanwhile, file_1.txt is already exist in /home/pungki/office directory.
$ mv -i file_1.txt /home/pungki/office
![mv with interactive mode](http://linoxide.com/wp-content/uploads/2014/01/mv_interactive.png)
This notification will aware us about the existence of file_1.txt in the destination location. If we press “**y**” then the file will be moved, otherwise, it will not.
### 8. Using update option ###
While -i are notify us about overwriting files, then -u option will perform update **only if the source is newer than destination file**. Lets take a look example below.
![Update only newer](http://linoxide.com/wp-content/uploads/2014/01/mv_u.png)
We have file_1.txt and file_2.txt with this attributes :
File_1.txt has 84 bytes file size and it last modified time is 12:00
File_2.txt has 0 bytes file size and it last modified time is 11:59
We want to move them into /home/pungki/office directory. **But in the destination location**, we already have file_1.txt and file_2.txt.
We move file_1.txt and file_2.txt from current directory into /home/pungki/office using command :
$ mv -uv *.txt /home/pungki/office
As the result, we see those files are moved. Those file is moved because their last modified time stamp is newer than the files in /home/pungki/office directory.
### 9. Do not overwrite any existing file ###
If **-i** options is asking us about overwriting files, than -n option will not allow us to overwrite any existing files.
Using example on point 8, if we change the option from **-u to -n**, combine with **-v** option, then we will see that there are no files moved into /home/pungki/office directory.
$ mv -vn *.txt /home/pungki/office
![No overwrite](http://linoxide.com/wp-content/uploads/2014/01/mv_n.png)
### 10. Create backup when copying ###
By default, moving files will overwrite the destination files if there are already exist before. But what happen if you are moving wrong files, and the destination files are already overwritten by the new ones? **Is there a way to retrieve the old one? Yes there is**. We can use **-b** option. **-b** option will make a backup of destination file before it overwritten by the new one. Once again, we will use scenario from point 8 above.
$ mv -bv *.txt /home/pungki/office
![Backup option](http://linoxide.com/wp-content/uploads/2014/01/mv_b.png)
As you can see on the screenshot, on the /home/pungki/office directory, we have a file named **file_1.txt~ and file_2.txt~ . The tilde sign (~) means** that those files are backup. We can see the attribute of them is older than file_1.txt and file_2.txt.
### Conclusion ###
Moving file or directory also one of the basic command in Linux system. As usual you can type **man mv** or **mv --help** to display its manual page to explore more detail.
--------------------------------------------------------------------------------
via: http://linoxide.com/linux-command/mv-command-linux/
译者:[译者ID](https://github.com/译者ID) 校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](http://linux.cn/) 荣誉推出
[1]:http://linoxide.com/linux-command/linux-cp-command/