Find the answer to your Linux question:
Results 1 to 3 of 3
Hello all am not very good at command's or even know it this is posable but am trying to do this "Create folder from a filename and move the file ...
  1. #1
    Just Joined! Vodkaholic's Avatar
    Join Date
    Feb 2011
    Posts
    2

    Create folder from a filename and move the file into the folder

    Hello all am not very good at command's or even know it this is posable but am trying to do this "Create folder from a filename and move the file into the folder" i have 500000+ file's i need to do with is there a easy way?

    I really don't want to download them all make/move them with filemonkey just to re-upload them

    Many Thanks

  2. #2
    Just Joined!
    Join Date
    Mar 2010
    Posts
    79
    You could run a short test like the following, and then look for a better way:

    Code:
    touch {one,two,three}.{sh,txt}
    Code:
    #!/usr/bin/env bash
    
    
    for i in  "$@"
    do
    	mkdir ${i}_folder
    	mv $i ${i}_folder
    done 
    
    
    exit 0
    Code:
    bash move_files.sh *sh
    or:
    Code:
    bash move_files.sh *txt
    Or both at once:
    Code:
    bash move_files.sh *txt *sh
    You will need to add several tests:
    Are arguments given
    Make sure the script itself doesn't get moved.
    Perhaps let the files get listed, and ask if it is ok to move them
    Check for filenames which contain whitepsace.
    Check for the exit code of `mkdir`
    Perhaps more.

  3. #3
    Just Joined! Vodkaholic's Avatar
    Join Date
    Feb 2011
    Posts
    2
    Quote Originally Posted by tornow View Post
    You could run a short test like the following, and then look for a better way:

    Code:
    touch {one,two,three}.{sh,txt}
    Code:
    #!/usr/bin/env bash
    
    
    for i in  "$@"
    do
    	mkdir ${i}_folder
    	mv $i ${i}_folder
    done 
    
    
    exit 0
    Code:
    bash move_files.sh *sh
    or:
    Code:
    bash move_files.sh *txt
    Or both at once:
    Code:
    bash move_files.sh *txt *sh
    You will need to add several tests:
    Are arguments given
    Make sure the script itself doesn't get moved.
    Perhaps let the files get listed, and ask if it is ok to move them
    Check for filenames which contain whitepsace.
    Check for the exit code of `mkdir`
    Perhaps more.
    Hey thank's for that

    as i know nothing someone made me a bash file which has this in it

    Code:
    #!/bin/bash
    for FILE in `ls | egrep "gif|jpg|png|bmp|jpeg|tif|tiff"`
    do
    DIR=`echo $FILE | cut -d '.' -f 1`
    mkdir $DIR
    mv $FILE $DIR
    done
    It kind of work's tho it split's the filename in to muilty folder's

    This was my test
    fred.jpg

    you say whitespace's everyone of my files have them :/ and i also need them for the next part after the folder's are made

    Thank's!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •