[Solved] Touch 10% of the total files inside a folder


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers [Solved] Touch 10% of the total files inside a folder
# 8  
Old 04-24-2012
sorry. the line i gave replaces the ls | while loop. this for loop will give $file a full path, so you can touch the file no matter your working directory.

Code:
#!/bash/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

N=0

for file in "/home/user/Desktop/"*; do
    [ -f "$file" ] || continue # Skip things which aren't files

    [ `expr $N % 10` -eq 0 ] && touch "$file"

    N=`expr $N + 1` # Add 1 to N
done

This User Gave Thanks to neutronscott For This Post:
# 9  
Old 04-24-2012
Many thanks guys! It works perfectly!

---------- Post updated at 05:10 PM ---------- Previous update was at 03:58 PM ----------

One last questions related to this topic! How do you make it scan for the 20% of the total files? I tried changin [ `expr $N % 10` -eq 0 ] to [ `expr $N % 20` -eq 0 ] but it touches less files now....

Thanks!

Quote:
Originally Posted by Corona688
What's your system? What's your shell?

Code:
N=0

ls /home/user/Desktop | while read FILE
do
        [ -f "$FILE" ] || continue # Skip things which aren't files

        # % is remainder of division.  0 % 10 is 0.  1 % 10 is 1.  9 % 10 is 9.  10 % 10 is 0.  And so on.
        # The remainder will be zero, every tenth file.
        [ `expr $N % 10` -eq 0 ] && touch "$FILE"

        N=`expr $N + 1` # Add 1 to N
done

# 10  
Old 04-24-2012
if you use 20 it will touch every 20th file and that's why number of new files are less.
for 20% use [ `expr $N % 5` -eq 0 ].
This User Gave Thanks to 47shailesh For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Counting total files with different file types in each folder

Trying to count total files with different file types with thousands of files in each folder. Since some files do not have extensions I have to use below criteria. Count Total Files starting with --> "^ERROR" Count Total Files starting with --> "^Runtime" Count Everything else or files... (3 Replies)
Discussion started by: kchinnam
3 Replies

2. UNIX for Dummies Questions & Answers

How to get all the files inside a folder then put the value in a variable?

Hi here is my code for i in `ls *.cmd` do msg aaa imp -U$AAAUSER -P$AAAUSERPWD <$i>>$curdir/import_tap.out -Jutf8 done I can only get all files with .cmd extension. what i need to get are all the files inside the specific folder Thanks (2 Replies)
Discussion started by: cmarzan
2 Replies

3. Shell Programming and Scripting

[Solved] Simple Shellscript for uploading files to a specific folder on a ftp-server?

hi! Iam using my D-link DNS-320 (NAS) with fun_plug installed (a unix client) I am currently using cron to run a shellscript running a java-application that creates a couple of txt files. These files needs to be uploaded to a specific folder on my webhosts ftp server-account. I need a... (16 Replies)
Discussion started by: Nigge
16 Replies

4. Shell Programming and Scripting

Total number of files in the folder should be listed

Hi All, When i give the ls -lrt to list out all files with total number of files , i get the output as ls -lrt total 72 -rw-r--r-- 1 hari staff 796 Jul 11 09:17 va.txt -rw-r--r-- 1 hari staff 169 Jul 13 00:20 a.log -rwxr-xr-x 1 hari staff 659 Aug... (9 Replies)
Discussion started by: Kalaihari
9 Replies

5. Shell Programming and Scripting

Renumber position 88-94 inside all files matching criteria inside folder

There are 4 files inside one folder matching criteria i.e. File name = ABCJmdmfbsjopXXXXXXX_mm-dd-yyyy_XXX.data Here is the Code which find the files matching criteria:- TS=`date +"%m-%d-%Y"`| for fname in `find . -name "ABCJmdmfbsjop???????_${TS}*.data"` do # Matching File Processing Code.... (1 Reply)
Discussion started by: lancesunny
1 Replies

6. Shell Programming and Scripting

Security for applets files inside a folder

Friends I have a directory structure /a/b/c/applets/ This directory has .java, .class and other applet files. I gave the applets folder 755 permission because these applets are displayed at different web pages of a portal. Now I want to restrict the visibility of all the files in this... (0 Replies)
Discussion started by: dahlia84
0 Replies

7. UNIX for Dummies Questions & Answers

I am trying to get the total size of a folder?

I am trying to get the total size of the folder using the below command but its not working. any ideas? du -bc <foldername>/|grep total|tr -s " "|cut -d" " -f1 the output i am getting is 78996 total but i just want it to be as 78996 please help (3 Replies)
Discussion started by: classic
3 Replies

8. Shell Programming and Scripting

check how many files in folder or total files in folder

Hi all, I have copied all my files to one folder.and i want to check how many files (count) in the folder recently moved or total files in my folder? please send me the query asap. (3 Replies)
Discussion started by: durgaprasad
3 Replies

9. UNIX for Dummies Questions & Answers

Touch all files and subdirectories (recursive touch)

I have a folder with many subdirectories and i need to set the modified date to today for everything in it. Please help, thanks! I tried something i found online, find . -print0 | xargs -r0 touch but I got the error: xargs: illegal option -- r (5 Replies)
Discussion started by: glev2005
5 Replies

10. UNIX for Dummies Questions & Answers

grep running total/ final total across multiple files

Ok, another fun hiccup in my UNIX learning curve. I am trying to count the number of occurrences of an IP address across multiple files named example.hits. I can extract the number of occurrences from the files individually but when you use grep -c with multiple files you get the output similar to... (5 Replies)
Discussion started by: MrAd
5 Replies
Login or Register to Ask a Question