linux - List out text files from given directory in unix


Question: 

Writing shell script to sort text file data from given directory with for loop in ubuntu as:

   echo "Enter directory Name"
   read dr
   path= ` find /home/user -name $dr `
   for x in ` ls *.txt $path ` 
     do
       sort $x
     done
   echo "------Script finished-------"

But this script is giving me error or message like(even tho' i have text files):

   ls: cannot access *.txt: No Such File or Directoy

I think there is problem with ls command,Is it syntactically wrong or what ? How to list out text files from given directory path ?




1 Answer: 

Here is a modified example. I do not know why backticks did not work so I used ():

echo "Enter directory Name"
   read dr
   path=( $(find /home/user -type d -name $dr))
   for x in `ls $path/*.txt`
     do
       sort $x
     done
   echo "------Script finished-------"
 

More Articles


php - Ubuntu Server Virtual Host not redirecting properly

I have installed Web site on my Ubuntu Server. The following things are installed on my Server for running web service. 1) Apache22) PHP53) MySql My main website is redirected to https.Example (changed for security purpose) <VirtualHost *.80>ServerName www.example.comServerAlias example.co

shell - UNIX: copy entire directory structure and only specific file type

How i can recreate entire directory structure and copy only files with .txt extension if present. I would like to copy the directory or sub-directory even if its empty.

How do I insert PHP variables in a MySQL table?

This question already has an answer here: Reference: What is variable scope, which variables are accessible from where and what are “undefined variable” errors? 3 answers


node.js - Ubuntu + Nodejs + curl: (52) Empty reply from server

I have a nodejs process listening a port on my server:app.get('/statistic', function(req, res) { log_get_connection(req); getStatistic().then(statistic => { res.json(statistic); });});When I try to access it via curl being on the same machine:curl "http://127.0

Unable to Connect to mysql db through php in GCE instance with ubuntu

I'm a little bit frustrated with a problem I'm having in a system migration from AWS to GCE.I've an ec-2 instance running on amazon web services, perfectly working and configured.The problem is that google is offering a 100.000$ credit in a special plan for Startups, my company's CEO asks for a migr

linux - Segmentation fault due to lack of memory in C

This code gives me segmentation fault about 1/2 of the time:int main(int argc, char **argv) { float test[2619560]; int i; for(i = 0; i < 2619560; i++) test[i] = 1.0f;}I actually need to allocate a much larger array, is there some way of allowing the operating system to allow me ge


Unable to install LAMP (Linux, Apache, Mysql, PHP stack) on Ubuntu 16.04

I am trying to install LAMP server on my Ubuntu desktop. The problem I am facing is related to IP address of my laptop.While Setting Global ServerName to Suppress Syntax Warnings, as the documentation says here, when I am adding a ServerName directive, pointing to my server's public IP address in co

linux - filter out content between two files

I have two files conditions.txtabcdefghlogs.txtefghijklmnopqrstI am expecting output to be:ijklmnopqrstActual output:efghijklijklmnopmnopqrstqrstHere's the code I had worked till nowfunc(){while read conditiondoif [[ $line = $condition ]] ; then:elseecho "$line";done < condition.txt}while read li

c - Why do I get a seg fault on Ubuntu but not mac?

I have a program that checks the modification time of a file and executes the file if it has changed. Currently it works if I run it on my mac, but it seg faults if I run it on ubuntu. Please help me.note: this is in c#include <unistd.h>#include <stdlib.h>#include <stdio.h>#inclu

php - Page not found error on apache server on ubuntu 14.04

I have a project which i developed using WAMP. The project works fine as expected. I tried to use the same project on LAMP stack (Ubuntu 14.04). I imported the database to mysql server and then copied the files to /var/www/html folder and gave permissions using chmod to all files in www folder. When