Sponsored Content
Full Discussion: need help understanding mv
Top Forums Shell Programming and Scripting need help understanding mv Post 302356985 by methyl on Monday 28th of September 2009 10:38:22 AM
Old 09-28-2009
You should by now have a space after "[" and a space before "]".

The logic in this bit needs attention if you want to swap the filenames:

Quote:
set tmp = $file1
mv "$file2" "$file1"
mv "$tmp" "$file2"
Code:
Maybe try:
        # Rename both files to preserve their permissions.
        mv "${file1}" "${file1}.TMP"
        mv "${file2}" "${file2}.TMP"
        # Cross-rename the files to their final names
        mv "${file1}.TMP" "${file2}"
        mv "${file2}.TMP" "${file1}"


BTW. I can't make "set tmp = $file1" do anything useful.

Some may argue that the filename swap can be done in three steps (not four). However this can introduce unexpected changes to file permissions.

Footnote: Notice the curly braces. You will rarely find cause to not use them!

Footnote 2: Providing both files are on the same filesystem a "mv" command just changes the name of the inode. It is very quick and the contents of the files do not move. If the files are on different filesystems they do actually move!

Last edited by methyl; 09-28-2009 at 11:47 AM.. Reason: Footnote
 

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need help for understanding of script

# sub: find block (in cols), return line-numbers (begin-end) or 0 if notfound sub findb{ my ($exp1,$col1,$exp2,$col2)= @_; # $exp = expression to find, $col - column to search in my $cnt=0; my ($val1,$val2); my ($beg,$end); for($cnt=1;$cnt<=65536;$cnt++){ $val1 =... (3 Replies)
Discussion started by: suvenduperl
3 Replies

2. UNIX for Dummies Questions & Answers

understanding {%/*}/

Hi Gurus: I am trying to understand the following line of code.I did enough of googling to understand but no luck.Please help me understand the follow chunk of code: X=$0 MOD=${X%/*}/env.ksh X is the current script from which I am trying to execute. Say if X=test.ksh $MOD is echoing :... (3 Replies)
Discussion started by: vemana
3 Replies

3. Solaris

Understanding 'du' command

Hi I have a questions related 2 commands : 'du' and 'ls'. Why is the difference between output of 'du' and 'ls' cmd's ? Command 'du' : ------------------ jakubn@server1 /home/jakubn $ du -s * 4 engine.ksh 1331 scripts 'du -s *' ---> shows block count size on disk (512 Bytes... (5 Replies)
Discussion started by: presul
5 Replies

4. Shell Programming and Scripting

Understanding <<EOF

Hi all I stuck with a problem. I want to understand the execution of the below code. Can any one please help me `sqlplus username/passwd@DB << EOF set serveroutput on declare begin sql_query; end; / commit / quit EOF` My ques is why do we use EOF and how does it help. (4 Replies)
Discussion started by: parthmittal2007
4 Replies

5. Shell Programming and Scripting

Understanding a regex

Hi, Please help me to understand the bold segments in the below regex. Both are of same type whose meaning I am looking for. find . \( -iregex './\{6,10\}./src' \) -type d -maxdepth 2 Output: ./20111210.0/src In continuation to above: sed -e 's|./\(*.\{1,3\}\).*|\1|g' Output: ... (4 Replies)
Discussion started by: vibhor_agarwali
4 Replies

6. Shell Programming and Scripting

Help understanding the script

Hi Guys, I am new to scripting , I am trying to rebuild a script based on an old script. Can someone help me figure out what the script is doing? This is only a part of the script. I am looking to interpret these two points in the scripts:- 1) test=`echo $?` while I do not... (3 Replies)
Discussion started by: rajsan
3 Replies

7. Shell Programming and Scripting

Help with understanding of alias

Hi, I saw the following explanation about alias in bash from gnu website, but I didn't get the meaning: Bash always reads at least one complete line of input before executing any of the commands on that line. Aliases are expanded when a command is read, not when it is executed. Therefore, an... (3 Replies)
Discussion started by: Roy987
3 Replies

8. UNIX for Dummies Questions & Answers

Understanding lseek

I tried to use lseek system call to determine the number of bytes in a file. To do so, I used open system call with O_APPEND flag to open a file. As lseek returns the current offset so I called lseek for opened file with offset as zero and whence as SEEK_CUR. So I guess it must return the number of... (3 Replies)
Discussion started by: Deepak Raj
3 Replies

9. Shell Programming and Scripting

Need your help in understanding this

Hi, I found this in a script and I would like to know how this works Code is here: # var1=PART1_PART2 # var2=${var1##*_} # echo $var2 PART2 I'm wondering how ##* makes the Shell to understand to pick up the last value from the given. (2 Replies)
Discussion started by: sathyaonnuix
2 Replies
DIFF(1) 						      General Commands Manual							   DIFF(1)

NAME
diff - print differences between two files SYNOPSIS
diff [-c | -e | -C n] [-br]file1 file2 OPTIONS
-C n Produce output that contains n lines of context -b Ignore white space when comparing -c Produce output that contains three lines of context -e Produce an ed-script to convert file1 into file2 -r Apply diff recursively to files and directories of EXAMPLES
diff file1 file2 # Print differences between 2 files diff -C 0 file1 file2 # Same as above diff -C 3 file1 file2 # Output three lines of context with every diff -c file1 file2 # Same diff /etc /dev # Compares recursively the directories /etc and /dev diff passwd /etc # Compares ./passwd to /etc/passwd DESCRIPTION
the same name, when file1 and file2 are both directories" difference encountered" Diff compares two files and generates a list of lines telling how the two files differ. Lines may not be longer than 128 characters. If the two arguments on the command line are both directories, diff recursively steps through all subdirectories comparing files of the same name. If a file name is found only in one directory, a diagnostic message is written to stdout. A file that is of either block special, character special or FIFO special type, cannot be compared to any other file. On the other hand, if there is one directory and one file given on the command line, diff tries to compare the file with the same name as file in the directory directory. SEE ALSO
cdiff(1), cmp(1), comm(1), patch(1). DIFF(1)
All times are GMT -4. The time now is 02:53 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy