Visit Our UNIX and Linux User Community


[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
# 1  
Old 04-24-2012
[Solved] Touch 10% of the total files inside a folder

I'm trying to make this code below to work but I can't find the way to do the following: I want to make the script to touch only 10% of the total amount of files counted inside the given directory instead of all like it is now.

I would greatly appreciate it if someone can give me a direction on how to make this behavior work.

Many thanks.
______________________________________________________

Code:
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
FILES=/home/user/Desktop/*

for file in $FILES
do
   touch "$file"
done


Moderator's Comments:
Mod Comment Please use next time
code tags for your code and data
# 2  
Old 04-24-2012
How do you want it to "touch" only 10%, on what criteria?
In the simplest way,you would need to count how many, and compute to get 10%, then use a counter to get out of the loop
# 3  
Old 04-24-2012
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

This User Gave Thanks to Corona688 For This Post:
# 4  
Old 04-24-2012
Im using CentOS and Im executing this script with a crontab! MANY THANKS!

---------- Post updated at 01:45 PM ---------- Previous update was at 01:11 PM ----------

Thank you Corona 688! The script works great when executed from the CLI but when I place the "touch.sh" file into the bin folder where the cronjob was grabbing the old script it doesn't make any changes.

Any Idea?

Thanks Again!
# 5  
Old 04-24-2012
The usual culprit with cron, so common its in our FAQ, is that common commands may not be in the PATH. Source /etc/profile, or set a hardcoded PATH you're sure encompasses everything you need.
# 6  
Old 04-24-2012
It's the working directory. Use full paths or change directory.

Code:
for file in "/home/user/Desktop/"*; do
    ...
done

This User Gave Thanks to neutronscott For This Post:
# 7  
Old 04-24-2012
hi neutronscott, thanks for you input but I can't get it to work with your suggestion.

Find the script below provided by Corona688 with the integration of you line.

___________
crontab below:
Code:
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

* * * * * /bin/bash /home/user/bin/touch.sh

touch.sh file below:
Code:
#-------------------------------------------------------------------------------
# Touch 10% of the files inside a directory
#-------------------------------------------------------------------------------

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

N=0

for file in "/home/user/Desktop/"*;do 
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


Last edited by Scott; 04-24-2012 at 05:25 PM.. Reason: Please use code tags
 

Previous Thread | Next Thread
Test Your Knowledge in Computers #51
Difficulty: Easy
The term 'IoT' means the 'Internet of Tomorrow'.
True or False?

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

Featured Tech Videos