Sponsored Content
Top Forums Shell Programming and Scripting Renaming a file name using awk Post 302415784 by Franklin52 on Friday 23rd of April 2010 10:06:52 AM
Old 04-23-2010
Try this, check the output first without the coloured part:
Code:
ls | awk -F\. -v d=$(date +"%Y%m%d") '
{f=$0;$1=$1 "-" d;$NF=""}{print "mv " f " " $0}
' OFS="." | sh

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Renaming a file name

I have an audit log that is produced each day from a production printer. It names the file using todays date, but it removes the leading zero's. For example: todays date 060104 names the file 6104.txt. I ftp this file onto a Sun box and pull stats off of it. To keep some consistency to what I am... (2 Replies)
Discussion started by: dbrundrett
2 Replies

2. UNIX for Dummies Questions & Answers

Help in renaming file !!!

Hi All, I want to rename a file inside a script which has a date portion appended at the start of the file name. The script i wrote works fine when the file comes on a day to day basis but sometimes it comes late too. #!/usr/bin/ksh cd /space/file/source dt=$(date "date "+%m%d%Y")... (5 Replies)
Discussion started by: kumarsaravana_s
5 Replies

3. Shell Programming and Scripting

renaming file

Dear Friends, Need your help once again. I have this file name e.g.1) report_12.rp_1 e.g.2) remark_mm.rmr_3 I want it to be renamed as report_12_1.rp remark_mm_3.rmr (3 Replies)
Discussion started by: anushree.a
3 Replies

4. Shell Programming and Scripting

Renaming a file

I am a complete Unix newbie and I need some help! (Please...) I need to rename a file from the following format: Test_Test_EAR_1234.ear To the following: Test_Test_EAR.ear In other words, I need to remove everything after & including the final underscore up to the "." What is the best... (6 Replies)
Discussion started by: VeloLisa
6 Replies

5. Shell Programming and Scripting

Renaming a file use another file as a sequence calling a shl

have this shl that will FTP a file from the a directory in windows to UNIX, It get the name of the file stored in this variable $UpLoadFileName then put in the local directory LocalDir="${MPATH}/xxxxx/dat_files" that part seems to be working, but then I need to take that file and rename, I am using... (3 Replies)
Discussion started by: rechever
3 Replies

6. Shell Programming and Scripting

File renaming from list of names contained in another file

I have to rename a large number of files so that the name of each file corresponds to a code number that is given side by side in a list (textfile). The list contains in column A the filename of the actual files to be renamed and in column B the name (a client code, 9 digits) that has to be... (7 Replies)
Discussion started by: netfreighter
7 Replies

7. Shell Programming and Scripting

Renaming Movies (or Flipping Portions of Filenames Using sed or awk)

Hey folks My problem is simple. For my first stash of movies, I used a naming convention of YEAR_MOVIE_NAME__QUALITY/ for each movie folder. For example, if I had a 1080p print of Minority Report, it would be 2002_Minority_Report__1080p/. The 2nd time around, I changed the naming convention... (4 Replies)
Discussion started by: ksk
4 Replies

8. UNIX for Dummies Questions & Answers

renaming multiple files using sed or awk one liner

hi, I have a directory "test" under which there are 3 files a.txt,b.txt and c.txt. I need to rename those files to a.pl,b.pl and c.pl respectively. is it possible to achieve this in a sed or awk one liner? i have searched but many of them are scripts. I need to do this in a one liner. I... (2 Replies)
Discussion started by: pandeesh
2 Replies

9. Shell Programming and Scripting

Renaming file and check for the renamed file existence

Hi Am trying to move a file from one name to another When I do "ls" to check for the moved filename I can see the file but when I try the same with a script am unable.. I think am doing some pretty silly error.. please help.. toMove=`ls | grep -E "partition.+"` mv $toMove partition._org... (7 Replies)
Discussion started by: Priya Amaresh
7 Replies

10. Shell Programming and Scripting

Renaming the file

Hi guys, I have written a code to move my file from one directory to another directory, the file is in .csv format and i need to append a current date to the file. the prolem is date is getting appended after the file extension.. here is my code : cd /data/home/abc/xyz now=$(date... (7 Replies)
Discussion started by: azherkn3
7 Replies
RENAME(P)						     POSIX Programmer's Manual							 RENAME(P)

NAME
rename - rename a file SYNOPSIS
#include <stdio.h> int rename(const char *old, const char *new); DESCRIPTION
The rename() function shall change the name of a file. The old argument points to the pathname of the file to be renamed. The new argument points to the new pathname of the file. If either the old or new argument names a symbolic link, rename() shall operate on the symbolic link itself, and shall not resolve the last component of the argument. If the old argument and the new argument resolve to the same existing file, rename() shall return successfully and perform no other action. If the old argument points to the pathname of a file that is not a directory, the new argument shall not point to the pathname of a direc- tory. If the link named by the new argument exists, it shall be removed and old renamed to new. In this case, a link named new shall remain visible to other processes throughout the renaming operation and refer either to the file referred to by new or old before the operation began. Write access permission is required for both the directory containing old and the directory containing new. If the old argument points to the pathname of a directory, the new argument shall not point to the pathname of a file that is not a direc- tory. If the directory named by the new argument exists, it shall be removed and old renamed to new. In this case, a link named new shall exist throughout the renaming operation and shall refer either to the directory referred to by new or old before the operation began. If new names an existing directory, it shall be required to be an empty directory. If the old argument points to a pathname of a symbolic link, the symbolic link shall be renamed. If the new argument points to a pathname of a symbolic link, the symbolic link shall be removed. The new pathname shall not contain a path prefix that names old. Write access permission is required for the directory containing old and the directory containing new. If the old argument points to the pathname of a directory, write access permission may be required for the directory named by old, and, if it exists, the directory named by new. If the link named by the new argument exists and the file's link count becomes 0 when it is removed and no process has the file open, the space occupied by the file shall be freed and the file shall no longer be accessible. If one or more processes have the file open when the last link is removed, the link shall be removed before rename() returns, but the removal of the file contents shall be postponed until all references to the file are closed. Upon successful completion, rename() shall mark for update the st_ctime and st_mtime fields of the parent directory of each file. If the rename() function fails for any reason other than [EIO], any file named by new shall be unaffected. RETURN VALUE
Upon successful completion, rename() shall return 0; otherwise, -1 shall be returned, errno shall be set to indicate the error, and nei- ther the file named by old nor the file named by new shall be changed or created. ERRORS
The rename() function shall fail if: EACCES A component of either path prefix denies search permission; or one of the directories containing old or new denies write permis- sions; or, write permission is required and is denied for a directory pointed to by the old or new arguments. EBUSY The directory named by old or new is currently in use by the system or another process, and the implementation considers this an error. EEXIST or ENOTEMPTY The link named by new is a directory that is not an empty directory. EINVAL The new directory pathname contains a path prefix that names the old directory. EIO A physical I/O error has occurred. EISDIR The new argument points to a directory and the old argument points to a file that is not a directory. ELOOP A loop exists in symbolic links encountered during resolution of the path argument. EMLINK The file named by old is a directory, and the link count of the parent directory of new would exceed {LINK_MAX}. ENAMETOOLONG The length of the old or new argument exceeds {PATH_MAX} or a pathname component is longer than {NAME_MAX}. ENOENT The link named by old does not name an existing file, or either old or new points to an empty string. ENOSPC The directory that would contain new cannot be extended. ENOTDIR A component of either path prefix is not a directory; or the old argument names a directory and new argument names a non-directory file. EPERM or EACCES The S_ISVTX flag is set on the directory containing the file referred to by old and the caller is not the file owner, nor is the caller the directory owner, nor does the caller have appropriate privileges; or new refers to an existing file, the S_ISVTX flag is set on the directory containing this file, and the caller is not the file owner, nor is the caller the directory owner, nor does the caller have appropriate privileges. EROFS The requested operation requires writing in a directory on a read-only file system. EXDEV The links named by new and old are on different file systems and the implementation does not support links between file systems. The rename() function may fail if: EBUSY The file named by the old or new arguments is a named STREAM. ELOOP More than {SYMLOOP_MAX} symbolic links were encountered during resolution of the path argument. ENAMETOOLONG As a result of encountering a symbolic link in resolution of the path argument, the length of the substituted pathname string exceeded {PATH_MAX}. ETXTBSY The file to be renamed is a pure procedure (shared text) file that is being executed. The following sections are informative. EXAMPLES
Renaming a File The following example shows how to rename a file named /home/cnd/mod1 to /home/cnd/mod2. #include <stdio.h> int status; ... status = rename("/home/cnd/mod1", "/home/cnd/mod2"); APPLICATION USAGE
Some implementations mark for update the st_ctime field of renamed files and some do not. Applications which make use of the st_ctime field may behave differently with respect to renamed files unless they are designed to allow for either behavior. RATIONALE
This rename() function is equivalent for regular files to that defined by the ISO C standard. Its inclusion here expands that definition to include actions on directories and specifies behavior when the new parameter names a file that already exists. That specification requires that the action of the function be atomic. One of the reasons for introducing this function was to have a means of renaming directories while permitting implementations to prohibit the use of link() and unlink() with directories, thus constraining links to directories to those made by mkdir(). The specification that if old and new refer to the same file is intended to guarantee that: rename("x", "x"); does not remove the file. Renaming dot or dot-dot is prohibited in order to prevent cyclical file system paths. See also the descriptions of [ENOTEMPTY] and [ENAMETOOLONG] in rmdir() and [EBUSY] in unlink() . For a discussion of [EXDEV], see link() . FUTURE DIRECTIONS
None. SEE ALSO
link() , rmdir() , symlink() , unlink() , the Base Definitions volume of IEEE Std 1003.1-2001, <stdio.h> COPYRIGHT
Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1, 2003 Edition, Standard for Information Technol- ogy -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html . IEEE
/The Open Group 2003 RENAME(P)
All times are GMT -4. The time now is 10:36 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy