Sponsored Content
Top Forums Shell Programming and Scripting Moving files based on file name Post 302831411 by vidyadhar85 on Thursday 11th of July 2013 02:21:42 AM
Old 07-11-2013
so the filename format is the same as what you mentioned? Please specify your req correctly otherwise we would end up doing guess work and you will never get the desired answer Smilie

if server name is at field 2 (underscroe _ seperated)

Code:
 
for file in * ; do
dir_name=`echo $file | awk -F_ '{print $2}'`
# Create directories here if they are not there already
mv $file /home/username/temp/hosts/${dir_name}
done

 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Moving files based on creation date

Howdy, I'm trying to figure out how to move multiple files based on their creation date. If anyone can enlighten me it would be most appreciated!! Thanks! :D (1 Reply)
Discussion started by: dgoyea
1 Replies

2. Shell Programming and Scripting

Moving the files based on count and time.

Hi, I have a requirement ,let us say 1000 files needs to be transferred in an hour from one path to another path and if the files (1000 files) are transferred within an hour ( say 40 mins), then the process should remain idle for the remaining time ( 20 mins). (3 Replies)
Discussion started by: Asaikarthik
3 Replies

3. UNIX for Dummies Questions & Answers

finding and moving files based on the last three numerical characters in the filename

Hi, I have a series of files (upwards of 500) the filename format is as follows CC10-1234P1999.WGS84.p190, all in one directory. Now the last three numeric characters, in this case 999, can be anything from 001 to 999. I need to move some of them to a seperate directory, the ones I need to... (5 Replies)
Discussion started by: roche.j.mike
5 Replies

4. UNIX for Dummies Questions & Answers

moving files based on condition

hi i have to move files and send an email and attached the bad files to inform the developer about that. #!/bin/ksh BASE_DIR=/data/SrcFiles cd $BASE_DIR ## finding the files from work directory which are changed in 1 day find -type f -name "*.csv" –ctime 0 > /home/mydir/flist.txt ##... (14 Replies)
Discussion started by: awais290
14 Replies

5. UNIX for Advanced & Expert Users

Moving multiple files based on the pattern

I want to search for a particular file name patterns and move them to a specific folder, is it possible to do it with awk or sed? (1 Reply)
Discussion started by: rudoraj
1 Replies

6. Shell Programming and Scripting

Moving files from one directory to another based on 2 date variables

Hi All, I am currently coding for a requirement(LINUX OS) where I am supposed to move a file (Lets Call it Employee.txt) from Directory A to Directory B based on 2 date fields as below, Date_Current = 20120620 Date_Previous = 20120610 Source Directory : /iis_data/source Target... (11 Replies)
Discussion started by: dsfreddie
11 Replies

7. UNIX for Dummies Questions & Answers

Script moving files based on date

Hi, I need a script that moves files based on date to a folder. The folder should be created based on file date. Example is : Date file name ----- -------- Oct 08 07:39 10112012_073952.xls Oct 09 07:39 10112012_073952.xls Oct 10 07:39 ... (6 Replies)
Discussion started by: rockingvj
6 Replies

8. Shell Programming and Scripting

Moving files based on file creation

Hi, I have a directory having so many number of files. Now I want to move the files which are older than one month (lets say) from this directory to another directory (say BKP dir). Simply, if file is olderthan one month move it from source1 dir to BKP1 dir. My file names doesn't have... (7 Replies)
Discussion started by: karumudi7
7 Replies

9. Shell Programming and Scripting

Moving files based on size (string to integer)

I have a log file that I want to archive out as it reaches 100MB. I am using the following to get the file size into a variable but get the error "line 5: filesize=$(wc -c < logfile.log) if then echo "is greater than 100M" else echo "is less than 100M" fi I'm sure there's something... (2 Replies)
Discussion started by: Flakman
2 Replies

10. Shell Programming and Scripting

Moving old files based on pattern

Hi all I am trying to loop through a directory of files using a given search pattern. some of the files will be duplicated due to the pattern, but of the duplicate files i wanted to move the older files to another location. Is there any straightforward way of doing this ? One of ways I... (1 Reply)
Discussion started by: sthapa
1 Replies
opendir(3)						     Library Functions Manual							opendir(3)

NAME
opendir, readdir, readdir_r, telldir, seekdir, rewinddir, closedir - Performs operations on directories LIBRARY
Standard C Library (libc) SYNOPSIS
#include <sys/types.h> #include <dirent.h> DIR *opendir ( const char *dir_name ); struct dirent *readdir ( DIR *dir_pointer ); int readdir_r ( DIR *dir_pointer, struct dirent *entry, struct dirent **result); long telldir ( DIR *dir_pointer ); void seekdir ( DIR *dir_pointer, long location ); void rewinddir ( DIR *dir_pointer ); int closedir ( DIR *dir_pointer ); The following function does not conform to current industry standards and is supported only for backward compatibility: int readdir_r ( DIR *dir_pointer, struct dirent *result ); STANDARDS
Interfaces documented on this reference page conform to industry standards as follows: closedir(): POSIX.1, XPG4, XPG4-UNIX opendir(): POSIX.1, XPG4, XPG4-UNIX readdir(): POSIX.1c, XPG4, XPG4-UNIX readdir_r(): POSIX.1c rewinddir(): POSIX.1, XPG4, XPG4-UNIX seekdir(): POSIX.1, XPG4, XPG4-UNIX telldir(): POSIX.1, XPG4, XPG4-UNIX Refer to the standards(5) reference page for more information about industry standards and associated tags. PARAMETERS
Names the directory. If the final component of dir_name names a symbolic link, the link will be traversed and pathname resolution will continue. Points to the dir structure of an open directory. Points to the storage location that will hold the entry. Specifies the num- ber of an entry relative to the start of the directory. Contains the next directory entry on return from the readdir_r() function. Set equal to entry upon successful completion or to NULL on error or end-of-directory. DESCRIPTION
The opendir() function opens the directory designated by the dir_name parameter and associates a directory stream with it. The directory stream is positioned at the first entry. The type DIR, which is defined in the dirent.h header file, represents a directory stream, which is an ordered sequence of all the directory entries in a particular directory. If a file descriptor is used, the FD_CLOEXEC flag will be set on that file descriptor. The opendir() function also returns a pointer to identify the directory stream in subsequent operations. The null pointer is returned when the directory named by the dir_name parameter cannot be accessed or when not enough memory is available to hold the entire stream. The type DIR, which is defined in the dirent.h header file, represents a directory stream, which is an ordered sequence of all the direc- tory entries in a particular directory. Directory entries represent files; files may be removed from a directory or added to a directory asynchronously to the operation of the readdir() function. The readdir() function returns a pointer to a structure representing the directory entry at the current position in the directory stream specified by the dir_pointer parameter, and positions the directory stream at the next entry. It returns a null pointer upon reaching the end of the directory stream. The dirent structure defined in the dirent.h header file describes a directory entry. The readdir() function will not return directory entries containing empty names. If entries for . (dot) or .. (dot-dot) exist, one entry will be returned for . (dot) and one entry will be returned for .. (dot-dot); otherwise, they will not be returned. The pointer returned by the readdir() function points to data which may be overwritten by another call to readdir() on the same directory stream. This data will not be overwritten by another call to readdir() on a different directory stream. If a file is removed from or added to the directory after the most recent call to the opendir() or rewinddir() function, whether a subse- quent call to the readdir() function returns an entry for that file is unspecified. The readdir() function may buffer several directory entries per actual read operation; the readdir() function marks for update the st_atime field of the directory each time the directory is actually read. When it reaches the end of the directory, or when it detects an invalid seekdir() operation, the readdir() function returns the null value. The telldir() function returns the current location associated with the specified directory stream. The seekdir() function sets the position of the next readdir() operation on the directory stream specified by the dir_pointer parameter to the position specified by the location parameter. If the value of the location parameter was not returned by a call to the telldir() function, or if there was an intervening call to the rewinddir() function on this directory stream, the effect is undefined. The new position reverts to the one associated with the directory stream when the telldir() operation was performed. An attempt to seek to an invalid location causes the readdir() function to return the null value the next time it is called. The position should be that returned by a previous telldir() function call. The rewinddir() function resets the position of the specified directory stream to the beginning of the directory. It also causes the directory stream to refer to the current state of the corresponding directory, as a call to the opendir() function would have done. If the dir_pointer parameter does not refer to a directory stream, the effect is undefined. The closedir() function closes a directory stream and frees the structure associated with the dir_pointer parameter. Upon return, the value of dir_pointer may no longer point to an accessible object of the type DIR. If a file descriptor is used to implement type DIR, that file descriptor will be closed. [POSIX] The readdir_r() function is the reentrant version of the readdir() function. The readdir_r() function stores the next directory entry at entry, and returns entry in result. On end-of-directory, NULL is stored in result and 0 (zero) is returned. NOTES
An open directory must always be closed with the closedir() function to ensure that the next attempt to open that directory is successful. The use of the seekdir() and telldir() functions is not recommended in the Tru64 UNIX operating system, as the results can be unpre- dictable. [POSIX] The readdir function is not supported for multithreaded applications. Instead, its reentrant equivalent, readdir_r, should be used with multiple threads. EXAMPLES
To search a directory for the entry name: len = strlen(name); dir_pointer = opendir("."); for (dp = readdir(dir_pointer); dp != NULL; dp = readdir(dir_pointer)) if (dp->d_namlen == len && !strcmp(dp->d_name, name)) { closedir(dir_pointer); return FOUND; } closedir(dir_pointer); return NOT_FOUND; RETURN VALUES
Upon successful completion, the opendir() function returns a pointer to an object of type DIR. Otherwise, null is returned and errno set to indicate the error. Upon successful completion, the readdir() function returns a pointer to an object of type struct dirent. When an error is encountered, a null pointer is returned and errno is set to indicate the error. When the end of the directory is encountered, a null pointer is returned and errno is not changed. Upon successful completion, the telldir() function returns the current location. Otherwise, -1 is returned. Upon successful completion, the closedir() function returns 0 (zero). Otherwise, -1 is returned. [POSIX] Upon successful completion or end-of-directory, the readd_r() function returns 0 (zero). Otherwise, an error number is returned. [Tru64 UNIX] Upon successful completion, the obsolete version of the readdir_r() function returns 0 (zero). Otherwise, -1 is returned. ERRORS
If the opendir() function fails, errno may be set to one of the following values: Search permission is denied for any component of dir_name or read permission is denied for dir_name. Too many links were encountered in translating dir_name. The length of the dir_name string exceeds PATH_MAX, or a pathname component is longer than NAME_MAX. The dir_name parameter points to the name of a file that does not exist, or the parameter points to an empty string. A component of dir_name is not a directory. RELATED INFORMATION
Functions: close(2), lseek(2), open(2), read(2), scandir(3) Standards: standards(5) delim off opendir(3)
All times are GMT -4. The time now is 09:32 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy