Enlightener
Posts: 4
Joined: Tue Apr 14, 2015 3:27 am

Arrays

Tue Apr 14, 2015 3:32 am

Hi guys! I am new here. Just recently go into doing arrays and scripting. I'm a little stuck on creating a script that can read the files of your home directory into an array by using the code arrayx=( $( ls. ) )
It also has to display only empty files, only directories, and only files that are not empty. Sorry I am new to this, and this just stumped me. I have been looking for a while and have not found anything. I also have to create a backup script that will copy a directory that we input to the script to another location that we input to the script. If that makes sense. Thanks in advance!

User avatar
rpdom
Posts: 17275
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Arrays

Tue Apr 14, 2015 4:05 am

Quite a few things there, try breaking it down a bit.

I'm assuming Bash is being used for all of these.
Create an arrary of everything in the current directory using ls

Code: Select all

x=( $( ls ) )
(hidden files will not be listed)
That far you already got.

Create an array of all empty files (0 length, real files) in current directory

Code: Select all

x=( $( find . -maxdepth 1 -size 0 -type f ) )
(Filenames will be prefixed with "./" and hidden files will be shown)

Create an array of all Non-empty files (not 0 length, real files) in current directory

Code: Select all

x=( $( find . -maxdepth 1 -size +0 -type f ) )
Create an array of all directories in current directory (just top-level)

Code: Select all

x=( $( find . -maxdepth 1 -type d ) )
Create an array of all directories in current directory and deeper, but only ones in the same filesystem.

Code: Select all

x=( $( find . -xdev -type d ) )
For backup scripts, look at the rsync command. It is very powerful for doing backups, both remote and local.

Enlightener
Posts: 4
Joined: Tue Apr 14, 2015 3:27 am

Re: Arrays

Tue Apr 14, 2015 4:22 am

Thanks! Sorry about not being so organized. I am doing this in nano using shell script. After typing those down. After running it did not seem to work. Am I missing something?

User avatar
rpdom
Posts: 17275
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Arrays

Tue Apr 14, 2015 4:45 am

What exactly are you expecting them to do?

They populate an array called "x" with the lists of names you asked for. Now it is up to you to do things with those arrays.

Here's an example to get you started:

Code: Select all

#!/bin/bash

# Get a list of non-empty files in the current directory
# with the "./" removed from the start of the lines
filelist=( $( find . -maxdepth 1 -size +0 -type f | cut -c3- ) )

# Get the count of elements in the array
filecount=${#filelist[@]}
echo "There are $filecount non-empty files in the current directory."

# List the contents of the array
echo "They are:"
index=0
while [ $index -lt $filecount ]
do
  echo "${filelist[$index]}"
  ((index++))
done
Now search on "bash arrays" and you'll find a load more :)

Enlightener
Posts: 4
Joined: Tue Apr 14, 2015 3:27 am

Re: Arrays

Tue Apr 14, 2015 4:55 am

Thank you!

Return to “Other programming languages”