LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-01-2011, 11:50 AM   #1
brunces
LQ Newbie
 
Registered: Nov 2011
Posts: 7

Rep: Reputation: Disabled
Scripts to list folder contents and copy images from folder and subfolders


Friends,

I use Ubuntu 11.10 with its default file manager, Nautilus 3.2.1.

Please, if possible, I'd like someone gentle to create for me (yes, "create for me" because I'm still a beginner on Linux to create it myself) two "batch" files (on Linux it's called "script", right?).

The first script, I want it to do the following...

1) I right-click a folder and, in the context menu, the words "Create list of contents" should appear.
2) After clicking "Create list of contents", it should generate a TXT file (into any folder, DOCUMENTS, for example) only with the names of the files and/or folders inside the clicked folder. Then, automatically, the generated TXT file should be opened (with Gedit, for example).

I have such thing on Windows. Actually, they are DOS commands, a batch file. It runs a DIR command in the clicked foder and sends the result of that DIR command to a TXT file. I don't know whether the logic for Linux is the same. Just for illustration, take a look at the batch file I use on Windows:

Batch file name: CreateList.bat
Code:
@echo off
cd %1

:checkfile
dir List.txt
if errorlevel 1 goto :checktempfile

:deletefile
del List.txt /q

:checktempfile
dir %USERPROFILE%\AppData\Local\Temp\List.txt
if errorlevel 1 goto :listcontents

:deletetempfile
del %USERPROFILE%\AppData\Local\Temp\List.txt /q

:listcontents
echo %~f1
dir /a /b /-p /o:gen > %USERPROFILE%\AppData\Local\Temp\List.txt
copy %USERPROFILE%\AppData\Local\Temp\List.txt %1
start "" List.txt
This is how it works on Windows:

1) I have this batch file in the folder: "D:\Batches\CreateList.bat"
2) I have an option "Create list of contents" in the folder context menu of Windows Explorer.
3) When I right-click any folder and then click on "Create list of contents" in the context menu, it runs that "D:\Batches\CreateList.bat" file.

I don't know whether the logic for Linux is the same, but if so, it would be something like:

1) A script file in a folder: "/home/Scripts/CreateList.sh"
2) An option "Create list of contents" in the folder context menu of Nautilus.
3) When I right-click any folder and then click on "Create list of contents" in the context menu, it runs that "/home/Scripts/CreateList.sh" file.

This is "my" logic, for I don't understand about Linux. Maybe, on Linux, the process is much simpler. Maybe the commands are put directly on Nautilus, I don't know. I don't understand.

Obs.: I use the same logic for the "CopyImages.bat".

The second script, I want it to do the following:

1) I right-click a folder and, in the context menu, the words "Copy image files" should appear.
2) After clicking "Copy image files", it should copy all image files of any type (JPG, BMP, PNG, etc.) from the clicked folder, including its subfolders (this is very important), into another one (any folder, DOCUMENTS, for example).

I have such thing on Windows, I mean, on DOS. Just for illustration, take a look at the batch file I use on Windows:

Batch file name: CopyImages.bat
Code:
@echo off
cd %1

:copyjpgs
for /r %%i in (*.jpg) do copy /y "%%i" "D:\Arquivos\Temp\Imagens"
Obs.: In this case, it copies only JPG files, but I want it to copy any image files.

As I said before, I need someone to create the script for me. I don't know how to do it. However, after I have a script already created, I can study its commands and see how the logic works and etc., then I can, who knows, create my own scripts, by myself.

The examples above refer only to the batch files, of course. To have those words in the context menu, I have two other files I've created to add those options to the Windows registry.

I thank very much any gentle person who can do me this favor.

brunces
 
Old 11-01-2011, 06:05 PM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,454

Rep: Reputation: 2052Reputation: 2052Reputation: 2052Reputation: 2052Reputation: 2052Reputation: 2052Reputation: 2052Reputation: 2052Reputation: 2052Reputation: 2052Reputation: 2052
Any chance you're willing to work on the command line? This will be much easier and much faster if you do that.

Your first script, if working on the command line, is:
Code:
ls > /home/user/list.txt
Your second script would be:
Code:
cp *.jpg *.png *.bmp /home/user/images/
I can count on one hand the number of times I've opened the GUI file manager on any of my Linux systems.

Last edited by suicidaleggroll; 11-01-2011 at 06:09 PM.
 
Old 11-03-2011, 04:32 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959
We usually don't appreciate being asked to simply write solutions for people. We aren't paid to act as tech support. But we do like helping people help themselves, and will generally do what we can to assist you, if you're willing to put in the effort to learn.

It's not hard to learn the basics of scripting. It just takes a little time and practice. Some good references can help too, of course.

These two guides are the best I know of for starting out:

http://mywiki.wooledge.org/BashGuide
http://www.linuxcommand.org/index.php

The Advanced Bash Scripting Guide (another good reference) even has a page on converting dos batch files to shell scripts.

http://www.tldp.org/LDP/abs/html/dosbatch.html

Take a bit of time and read completely through the first guide, at least. Then use the last one and try your hand at converting a script or two. You can post your results here and we can help you with any problems and give suggestions on how to handle things better.

Good luck!
 
Old 11-03-2011, 06:37 AM   #4
brunces
LQ Newbie
 
Registered: Nov 2011
Posts: 7

Original Poster
Rep: Reputation: Disabled
Guys,

Here's what I've come through up to now.

Name of the script: ListCont.sh
Code:
#!/bin/sh
ls --color=no > /home/brunces/Documents/list.txt
gedit /home/brunces/Documents/list.txt
Name of the script: CopyImgs.sh
Code:
#!/bin/sh
find -type f \( -iname '*.bmp' -o -iname '*.jpg' -o -iname '*.png' \) -execdir cp {} /home/brunces/Documents \;
It's working via terminal. My doubt now is how to put them in the folder context menu of Nautilus. I've searched about it and found Nautilus Actions. I've already installed it (Nautilus Actions v3.1.2-1), but it is not working. It seems there's a bug related to Ubuntu 11.10, but I'm not sure about it. Anyway, besides Nautilus Actions, is there any other way to get those two options ("List folder contents" and "Copy images") appearing in the context menu of Nautilus and, of course, each of them calling the scripts already created?

If anyone could help, I would really appreciate it. Thanks a lot.

Now, David the H., this is for you...

I understand your point and I won't controvert it. I just think what I've asked for (very politely, by the way) was very simple for you who is already a master on Linux and very difficult for me who is still a beginner. I didn't ask for an entire program code. All I needed was 3 command lines. Sometimes, the simpler tutorial isn't enough. I'm not saying people can't learn by themselves with good references. I'm just saying a "little" help from masters can be a "huge" help for beginners. Sometimes, telling people how to drive is not enough. You could, at least, "show" them how to drive. If books were enough, we wouldn't need schools and teachers.

I am not talking for others, I am talking for myself. It's my opinion. I'm a member of a well known forum, here in my country, and I answer questions related to Excel. I'm not a master on Excel, but sometimes I know things other people don't. And when they ask something I can answer, it's a pleasure for me to answer it. I'm not paid for that, of course, but I can say the person's gratefulness is my pay. I'm glad when I help. I never tell people to use Google, for example. If I don't know the answer or if I'm feeling lazy at the moment, I just don't answer. I never point people to tutorials unless I've already made a little example, answering the person's doubt. Then, I say, "Here's what you need. See the attached file. Then, go to this page. There you'll find a good tutorial on what you've asked for. Take a look at that tutorial to understand the code I've created for you." I'm not telling everybody should do that, but that's what "I" do.

Anyway, I appreciate your time, David the H., and the links you've given me. I've already taken a look at them and they really seem very good. I'll keep them carefully for later studies. Thank you.

brunces

Last edited by brunces; 11-03-2011 at 07:00 AM.
 
Old 11-03-2011, 08:22 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959
Not to worry. You don't appear to be lazy or freeloading to me. I'm just cautioning you that it's not a good idea to explicitly request things like writing entire scripts for you. While we might sometimes do that anyway, it sounds demanding if you ask for it directly. What you can generally expect are helpful suggestions, code examples, and links to sites with useful documentation. And if you post your own attempts at a solution we'll be glad to point out errors and help you make them work.

So I suggest you not say things like "please write it for me". Try instead "please show me how to do it", or "help me to write it", and post anything you've already tried. We can work together to solve your problems.


Now I'm not very familiar with nautilus, or how the n-a plugin works exactly, but I'm guesing your main issue is that you need to configure it to pass the proper arguments to the script, and to configure the script to accept the arguments. ls and find operate on the current working directory by default, but that may not be defined when the script is launched by nautilus.

To handle arguments in a script, use the "$1, $2, $3..." parameters. "$@" is a special parameter that refers to all the arguments. So for your first script, you might modify it like this:

Code:
#!/bin/sh
ls --color=no "$@" > /home/brunces/Documents/list.txt
gedit /home/brunces/Documents/list.txt
( By the way, do you want to hard-code the name of the text, or perhaps come up with some way to have a dynamic/temp filename? As it is, it will overwrite the contents every time you run it. )

Now we need to pass the current directory or selected files to the script. According to this page, nautilus actions use the desktop standard % variables. In this case %d for the current directory, and perhaps %F if you want to run it on a selection of files.

Try this in the command field of n-a:
Code:
ListCont.sh %d/%F
I'm not 100% sure that it will work as I wrote it, but it's probably close. The link also mentions %M, so you might try that instead.

Your find script should work similarly:

Code:
#!/bin/sh
find "$1" -type f \( -iname '*.bmp' -o -iname '*.jpg' -o -iname '*.png' \) -execdir cp {} /home/brunces/Documents \;

-----

CopyImgs.sh %d
Speaking of which, you do realize that find works recursively, and that every image file in every subdirectory will also be copied? You'll need to add -maxdepth 1 if you want to limit it to just the main directory.

It might also be possible to simplify it to just a globbing pattern, especially if you use bash.

Code:
#!/bin/bash

cp -t /home/brunces/Documents "$1"/**/*.{bmp,jpg,png}
**/ is a relatively new bash-specific globbing pattern which expands to a list of all subdirectories, so it should behave exactly the same thing as the find command. Remove that part if you only want it to operate on the current directory.


Let us know how it works out.
 
Old 11-03-2011, 12:12 PM   #6
brunces
LQ Newbie
 
Registered: Nov 2011
Posts: 7

Original Poster
Rep: Reputation: Disabled
David the H., thanks again for your time.

I'm at work right now. When I get home, I'll try your suggestions out. But thanks in advance.

brunces
 
Old 11-03-2011, 01:23 PM   #7
onebuck
Moderator
 
Registered: Jan 2005
Location: Midwest USA, Central Illinois
Distribution: Slackware®
Posts: 12,713
Blog Entries: 27

Rep: Reputation: 2055Reputation: 2055Reputation: 2055Reputation: 2055Reputation: 2055Reputation: 2055Reputation: 2055Reputation: 2055Reputation: 2055Reputation: 2055Reputation: 2055
Hi,

Welcome to LQ!

I concur that it would be best to aid you to learn with LQ members assistance. You should not expect fellow members to do the work. It is better if you attempt and request aid.

"Knowledge is of two kinds. We Know a subject ourselves, or we know where we can find information upon it."- Samuel Johnson


With the above quotation in mind then I suggest that
these links will aid you to gaining some understanding;



1 Linux Documentation Project
2 Rute Tutorial & Exposition
3 Linux Command Guide
4 Bash Beginners Guide
5 Bash Reference Manual
6 Advanced Bash-Scripting Guide
7 Linux Newbie Admin Guide
8 LinuxSelfHelp
9 Utimate Linux Newbie Guide
10 Linux Home Networking
11 Virtualization- Top 10

The above links and others can be found at 'Slackware-Links'. More than just Slackware® links!


The following links will help with your interaction here at LQ!

FYI: How to Answer a Linux Question is ‘Simon Bridge's excellent composition to aid us in good informational exchanges.'


FYI: Netiquette is a set of social conventions that facilitate interaction over networks, ranging from Usenet and mailing lists to blogs and forums.

 
  


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
Compress .gz my folder with all contens inside, subfolders,images.... ASTRAPI Linux - Newbie 4 10-05-2010 08:06 PM
want to copy an inode folder from acdrom to a home directory (folder). nhbob Linux - Newbie 8 12-20-2009 11:12 AM
Bash script to copy contents of folder brian.m Linux - General 8 05-01-2009 06:08 PM
LXer: Copy images in Linux from the iPhone’s pictures folder LXer Syndicated Linux News 0 11-10-2008 04:50 AM
Samba - XP can't list folder contents p0tw0r Linux - Networking 1 05-31-2005 03:37 AM


All times are GMT -5. The time now is 06:08 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration