LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 09-20-2020, 05:46 AM   #16
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,289
Blog Entries: 3

Rep: Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718

Quote:
Originally Posted by berndbausch View Post
You could, but only with a certain amount of contorsion.
A function would be rather easy.

Code:
#!/bin/bash

function checkdirectory() {
        filename=$1
        echo E=$filename
}

find . -type f -name '*' -print0 \
| while read -d '' file;
do
        checkdirectory $file
done
 
Old 09-20-2020, 05:51 AM   #17
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
Quote:
Originally Posted by Turbocapitalist View Post
A function would be rather easy.
I protest formally. This is not the same find command.

But of course you are right. You can put everything in the same file. I am sure there are many other ways for solving this.

I would like to warn OP. The script, if it works at all, only covers the narrow case that you described. If your problem is slightly different, you will have to rewrite the script.
 
Old 09-20-2020, 06:03 AM   #18
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,289
Blog Entries: 3

Rep: Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718
Quote:
Originally Posted by berndbausch View Post
I protest formally. This is not the same find command.
Quite right. It is not convenient to export a shell function into -exec in find.
 
Old 09-21-2020, 03:01 PM   #19
finalturismo
Member
 
Registered: May 2019
Posts: 120

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by berndbausch View Post
You could, but only with a certain amount of contorsion.

To avoid making a directory that exists already.


The ! in the first line is part of the syntax used to define the interpreter of this script.

The ! in the if condition negates the directory test, i.e. it means "not".

Contorsion, thats above my vocabulary capabilities even Google spell checker doesn't have that saved.

Makes you wonder if ancient canabalizm had any science behind it, like if i ate your brain would i gain any IQ points.

Anyway so i think iam going to setup a lab server for this so i dont blow out my 5+ TB of data XD.

Do you happen to have skype or anything? If so can you message it to me, i have been looking for a tutor and i dont mind paying a few bucks for some time.
 
Old 09-21-2020, 03:38 PM   #20
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,593

Rep: Reputation: 2544Reputation: 2544Reputation: 2544Reputation: 2544Reputation: 2544Reputation: 2544Reputation: 2544Reputation: 2544Reputation: 2544Reputation: 2544Reputation: 2544
Quote:
Originally Posted by finalturismo View Post
Contorsion, thats above my vocabulary capabilities even Google spell checker doesn't have that saved.
Contortion is twisting one's body into strange positions - an accurate analogy for a Bash script that executes a find command that calls the same Bash script again.

(If Google can't solve a single-letter ~sion -> ~tion misspelling, you might want to use a real spell checker next time.)

 
Old 09-22-2020, 01:52 AM   #21
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,786

Rep: Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304
I have never heard about contortion as a programming technique, but it is definitely a living method.

find itself is probably used to look for something and not for creating directories, moving files and/or executing complex tasks (although it is not impossible). find is basically a read-only tool (therefore this is the best tool for a contortionist).

The script in post #11 is almost complete:
Code:
for filename in *                 # use shell globbing and/or while - if you want to
do
    extension=${filename##*.}     # obtain extension
    mkdir -p "$extension"         # there is no need to check, mkdir -p will do that automatically
    mv $filename $extension
done
using find instead of this loop makes the execution definitely slower, that will be the indication of hard work.

In more complex cases (when more calculation/check required) I would recommend perl/python or similar.
 
Old 09-22-2020, 05:37 PM   #22
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,218

Rep: Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309
Yeah, I can't memorize the BASH string operators either.

Anyway, here' my attempt. It should be pretty efficient, but needs an up-to-date BASH.

Code:
#!/usr/bin/env bash

shopt -s globstar

declare -A extensions

for f in **/*; do
	if [ -f "$f" ]; then
		extensions["${f##*.}"]=1
	fi
done

for extension in "${!extensions[@]}"; do
	mkdir "$extension"
done

Last edited by dugan; 09-22-2020 at 06:26 PM.
 
Old 09-23-2020, 06:56 AM   #23
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
finalturismo, I don't know whether the Linuxquestions email feature is still working; I sent you a message but don't see it in the Sent folder. My Skype handle is like my Linuxquestions one.
 
Old 09-25-2020, 12:44 AM   #24
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,779

Rep: Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198
Let's ensure that our filenames have a .ext
Code:
find . -name "*.?*" -type f ...
Code:
for f in **/*.?* ; do if [ -f "$f" ] ; then ...
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How to remove all hidden directories and folders, and only hidden directories and folders rm_-rf_windows Linux - General 5 04-12-2016 07:28 AM
Find the directories which is older than x days delete and zip those directories ramesh pagadala Linux - Newbie 2 08-29-2013 08:17 AM
search all files and only empty directories using find command cli Linux - Newbie 19 06-14-2013 02:59 PM
Using the find command to list all files older than 2 years ifitz Linux - Newbie 2 12-01-2011 02:39 PM
[SOLVED] Find a file in directories without using find command sikanders Linux - Newbie 14 08-06-2010 08:47 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 06:12 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration