Overclock.net › Forums › Software, Programming and Coding › Operating Systems › Linux, Unix › More Linux Help (piping commands)
New Posts  All Forums:Forum Nav:

More Linux Help (piping commands)

post #1 of 10
Thread Starter 
OK, I need to use ls and the pipe "|" command to come up with a compound command that displays the number of files and folders in my current working directory. I'm stumped.
Kilo 3.0
(20 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5 2500K Gigabyte GA-Z68XP-UD3P HIS HD6970 1GB 8GB G.Skill RipJaws DDR3-1600 
Hard DriveHard DriveHard DriveHard Drive
OCZ Vertex 3 Seagate Barricuda Seagate GoFlex WD Passport 
Optical DriveOptical DriveCoolingCooling
Lite-On BD-RW ASUS DVD-RW Cooler Master H80 Arctic Cooling Twin Turbo II 
OSMonitorKeyboardPower
Windows 8 Pro 25" Hanns.G K710 Corsair TX750 
CaseMouseMouse PadAudio
Cooler Master 690 M710 XTrac Ripper Logitech 540 
  hide details  
Reply
Kilo 3.0
(20 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5 2500K Gigabyte GA-Z68XP-UD3P HIS HD6970 1GB 8GB G.Skill RipJaws DDR3-1600 
Hard DriveHard DriveHard DriveHard Drive
OCZ Vertex 3 Seagate Barricuda Seagate GoFlex WD Passport 
Optical DriveOptical DriveCoolingCooling
Lite-On BD-RW ASUS DVD-RW Cooler Master H80 Arctic Cooling Twin Turbo II 
OSMonitorKeyboardPower
Windows 8 Pro 25" Hanns.G K710 Corsair TX750 
CaseMouseMouse PadAudio
Cooler Master 690 M710 XTrac Ripper Logitech 540 
  hide details  
Reply
post #2 of 10
I would list the files in the current directory. I would have ls list all the files one per line. I would pipe that output to a program called wc which will count the lines = files.

Code:
user@asus64:~$ ls -1| wc -l
230
workstation
(14 items)
 
  
CPUMotherboardGraphicsRAM
i5 2500K EVGA Z68 SLI GeForce 670 GTX G.Skill 32GB 1600Mhz 
Hard DriveCoolingOSMonitor
OCZ Agility 3 60GB Zalman arch x86_x64 U3011 + U3014 
PowerCaseMouseAudio
PCPowerCooling 860w Chenbro server G500 Asus D2X, Def Tech 5.3 
  hide details  
Reply
workstation
(14 items)
 
  
CPUMotherboardGraphicsRAM
i5 2500K EVGA Z68 SLI GeForce 670 GTX G.Skill 32GB 1600Mhz 
Hard DriveCoolingOSMonitor
OCZ Agility 3 60GB Zalman arch x86_x64 U3011 + U3014 
PowerCaseMouseAudio
PCPowerCooling 860w Chenbro server G500 Asus D2X, Def Tech 5.3 
  hide details  
Reply
post #3 of 10
Thread Starter 
But how do you list the files and directories separately?
Kilo 3.0
(20 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5 2500K Gigabyte GA-Z68XP-UD3P HIS HD6970 1GB 8GB G.Skill RipJaws DDR3-1600 
Hard DriveHard DriveHard DriveHard Drive
OCZ Vertex 3 Seagate Barricuda Seagate GoFlex WD Passport 
Optical DriveOptical DriveCoolingCooling
Lite-On BD-RW ASUS DVD-RW Cooler Master H80 Arctic Cooling Twin Turbo II 
OSMonitorKeyboardPower
Windows 8 Pro 25" Hanns.G K710 Corsair TX750 
CaseMouseMouse PadAudio
Cooler Master 690 M710 XTrac Ripper Logitech 540 
  hide details  
Reply
Kilo 3.0
(20 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5 2500K Gigabyte GA-Z68XP-UD3P HIS HD6970 1GB 8GB G.Skill RipJaws DDR3-1600 
Hard DriveHard DriveHard DriveHard Drive
OCZ Vertex 3 Seagate Barricuda Seagate GoFlex WD Passport 
Optical DriveOptical DriveCoolingCooling
Lite-On BD-RW ASUS DVD-RW Cooler Master H80 Arctic Cooling Twin Turbo II 
OSMonitorKeyboardPower
Windows 8 Pro 25" Hanns.G K710 Corsair TX750 
CaseMouseMouse PadAudio
Cooler Master 690 M710 XTrac Ripper Logitech 540 
  hide details  
Reply
post #4 of 10
Code:
$ ls -l | grep "^d" | wc -l
will get you the number of directories.
Code:
$ ls -l | grep -v "^d" | wc -l
will get you the number of files.
post #5 of 10
Thread Starter 
Is there to list both with one piped command?
Kilo 3.0
(20 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5 2500K Gigabyte GA-Z68XP-UD3P HIS HD6970 1GB 8GB G.Skill RipJaws DDR3-1600 
Hard DriveHard DriveHard DriveHard Drive
OCZ Vertex 3 Seagate Barricuda Seagate GoFlex WD Passport 
Optical DriveOptical DriveCoolingCooling
Lite-On BD-RW ASUS DVD-RW Cooler Master H80 Arctic Cooling Twin Turbo II 
OSMonitorKeyboardPower
Windows 8 Pro 25" Hanns.G K710 Corsair TX750 
CaseMouseMouse PadAudio
Cooler Master 690 M710 XTrac Ripper Logitech 540 
  hide details  
Reply
Kilo 3.0
(20 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5 2500K Gigabyte GA-Z68XP-UD3P HIS HD6970 1GB 8GB G.Skill RipJaws DDR3-1600 
Hard DriveHard DriveHard DriveHard Drive
OCZ Vertex 3 Seagate Barricuda Seagate GoFlex WD Passport 
Optical DriveOptical DriveCoolingCooling
Lite-On BD-RW ASUS DVD-RW Cooler Master H80 Arctic Cooling Twin Turbo II 
OSMonitorKeyboardPower
Windows 8 Pro 25" Hanns.G K710 Corsair TX750 
CaseMouseMouse PadAudio
Cooler Master 690 M710 XTrac Ripper Logitech 540 
  hide details  
Reply
post #6 of 10
Quote:
Originally Posted by SIMPSONATOR View Post
Is there to list both with one piped command?
Not quite sure what you're asking, however,

Code:
$ ls -l | wc -l
will get you the number of files and directories

ls -l lists the contents of a directory each on a different line
wc -l evaluates stdinput and returns the number of lines
Edited by Jimi - 7/27/11 at 6:34pm
post #7 of 10
if you need counts of both files and directories separately using only one pipe... i'm sure there is a better way, but awk can always do whatever you want

ls -l | awk ' /^d/ {++d } !/^d/ {++f } END { printf("Directories: %d\
Files: %d\
Total: %d\
", d, f, d+f); } '

i cannot think of a better way but there has to be one...
Edited by lloyd mcclendon - 7/27/11 at 9:54pm
stable again
(25 items)
 
  
CPUCPUMotherboardGraphics
E5-2687W E5-2687W ASUS Z9PED8-WS EVGA GTX 570 (Linux host) 
GraphicsRAMHard DriveHard Drive
EVGA GTX 970 FTW (win7 guest) 64GB G.SKILL 2133 2x Crucial M4 256GB raid1 4x 3TB raid 10 
CoolingCoolingCoolingCooling
2x Apogee HD  2x RX 480 2x MCP 655 RP-452x2 rev2 (new) 
CoolingCoolingOSOS
16x Cougar Turbine CFT12SB4 (new) EK FC 580 Gentoo (host) Gentoo (x23 guests) 
OSMonitorMonitorPower
windows 7 (guest w/ vfio-pci) Viewsonic 23" 1080P Viewsonic 19" Antec HCP Platinum 1000 (new) 
CaseOtherOther
Case Labs TH10 (still the best ever) 2x Lamptron FC-5 IOGEAR 2 way DVI KVM Switch 
  hide details  
Reply
stable again
(25 items)
 
  
CPUCPUMotherboardGraphics
E5-2687W E5-2687W ASUS Z9PED8-WS EVGA GTX 570 (Linux host) 
GraphicsRAMHard DriveHard Drive
EVGA GTX 970 FTW (win7 guest) 64GB G.SKILL 2133 2x Crucial M4 256GB raid1 4x 3TB raid 10 
CoolingCoolingCoolingCooling
2x Apogee HD  2x RX 480 2x MCP 655 RP-452x2 rev2 (new) 
CoolingCoolingOSOS
16x Cougar Turbine CFT12SB4 (new) EK FC 580 Gentoo (host) Gentoo (x23 guests) 
OSMonitorMonitorPower
windows 7 (guest w/ vfio-pci) Viewsonic 23" 1080P Viewsonic 19" Antec HCP Platinum 1000 (new) 
CaseOtherOther
Case Labs TH10 (still the best ever) 2x Lamptron FC-5 IOGEAR 2 way DVI KVM Switch 
  hide details  
Reply
post #8 of 10
Thread Starter 
OK, I need to use 2 commands to create a file named filelog. The first command shows the first line below, and the second command creates the second line in the file, showing the current number of files in the director. I figured out part 1:


Command 1: echo File Log for `date` > filelog
Command 2: ??? I thing sed \\a is involved

File log for Thu Oct 10 12:02:35 PDT 2002
You currently have 4 files.


How do I do it?
Kilo 3.0
(20 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5 2500K Gigabyte GA-Z68XP-UD3P HIS HD6970 1GB 8GB G.Skill RipJaws DDR3-1600 
Hard DriveHard DriveHard DriveHard Drive
OCZ Vertex 3 Seagate Barricuda Seagate GoFlex WD Passport 
Optical DriveOptical DriveCoolingCooling
Lite-On BD-RW ASUS DVD-RW Cooler Master H80 Arctic Cooling Twin Turbo II 
OSMonitorKeyboardPower
Windows 8 Pro 25" Hanns.G K710 Corsair TX750 
CaseMouseMouse PadAudio
Cooler Master 690 M710 XTrac Ripper Logitech 540 
  hide details  
Reply
Kilo 3.0
(20 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5 2500K Gigabyte GA-Z68XP-UD3P HIS HD6970 1GB 8GB G.Skill RipJaws DDR3-1600 
Hard DriveHard DriveHard DriveHard Drive
OCZ Vertex 3 Seagate Barricuda Seagate GoFlex WD Passport 
Optical DriveOptical DriveCoolingCooling
Lite-On BD-RW ASUS DVD-RW Cooler Master H80 Arctic Cooling Twin Turbo II 
OSMonitorKeyboardPower
Windows 8 Pro 25" Hanns.G K710 Corsair TX750 
CaseMouseMouse PadAudio
Cooler Master 690 M710 XTrac Ripper Logitech 540 
  hide details  
Reply
post #9 of 10
Command 1: echo File Log for `date` > filelog
Command 2: echo You currently have `ls -l | grep -v ^d | wc -l` files >> filelog

One gap there is it will still say "files" instead of "file" if there is only one.

sure you could use sed, but >> to append to an existing file is easy enough
stable again
(25 items)
 
  
CPUCPUMotherboardGraphics
E5-2687W E5-2687W ASUS Z9PED8-WS EVGA GTX 570 (Linux host) 
GraphicsRAMHard DriveHard Drive
EVGA GTX 970 FTW (win7 guest) 64GB G.SKILL 2133 2x Crucial M4 256GB raid1 4x 3TB raid 10 
CoolingCoolingCoolingCooling
2x Apogee HD  2x RX 480 2x MCP 655 RP-452x2 rev2 (new) 
CoolingCoolingOSOS
16x Cougar Turbine CFT12SB4 (new) EK FC 580 Gentoo (host) Gentoo (x23 guests) 
OSMonitorMonitorPower
windows 7 (guest w/ vfio-pci) Viewsonic 23" 1080P Viewsonic 19" Antec HCP Platinum 1000 (new) 
CaseOtherOther
Case Labs TH10 (still the best ever) 2x Lamptron FC-5 IOGEAR 2 way DVI KVM Switch 
  hide details  
Reply
stable again
(25 items)
 
  
CPUCPUMotherboardGraphics
E5-2687W E5-2687W ASUS Z9PED8-WS EVGA GTX 570 (Linux host) 
GraphicsRAMHard DriveHard Drive
EVGA GTX 970 FTW (win7 guest) 64GB G.SKILL 2133 2x Crucial M4 256GB raid1 4x 3TB raid 10 
CoolingCoolingCoolingCooling
2x Apogee HD  2x RX 480 2x MCP 655 RP-452x2 rev2 (new) 
CoolingCoolingOSOS
16x Cougar Turbine CFT12SB4 (new) EK FC 580 Gentoo (host) Gentoo (x23 guests) 
OSMonitorMonitorPower
windows 7 (guest w/ vfio-pci) Viewsonic 23" 1080P Viewsonic 19" Antec HCP Platinum 1000 (new) 
CaseOtherOther
Case Labs TH10 (still the best ever) 2x Lamptron FC-5 IOGEAR 2 way DVI KVM Switch 
  hide details  
Reply
post #10 of 10
Thread Starter 
awesome, thanks!
Kilo 3.0
(20 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5 2500K Gigabyte GA-Z68XP-UD3P HIS HD6970 1GB 8GB G.Skill RipJaws DDR3-1600 
Hard DriveHard DriveHard DriveHard Drive
OCZ Vertex 3 Seagate Barricuda Seagate GoFlex WD Passport 
Optical DriveOptical DriveCoolingCooling
Lite-On BD-RW ASUS DVD-RW Cooler Master H80 Arctic Cooling Twin Turbo II 
OSMonitorKeyboardPower
Windows 8 Pro 25" Hanns.G K710 Corsair TX750 
CaseMouseMouse PadAudio
Cooler Master 690 M710 XTrac Ripper Logitech 540 
  hide details  
Reply
Kilo 3.0
(20 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5 2500K Gigabyte GA-Z68XP-UD3P HIS HD6970 1GB 8GB G.Skill RipJaws DDR3-1600 
Hard DriveHard DriveHard DriveHard Drive
OCZ Vertex 3 Seagate Barricuda Seagate GoFlex WD Passport 
Optical DriveOptical DriveCoolingCooling
Lite-On BD-RW ASUS DVD-RW Cooler Master H80 Arctic Cooling Twin Turbo II 
OSMonitorKeyboardPower
Windows 8 Pro 25" Hanns.G K710 Corsair TX750 
CaseMouseMouse PadAudio
Cooler Master 690 M710 XTrac Ripper Logitech 540 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Linux, Unix
Overclock.net › Forums › Software, Programming and Coding › Operating Systems › Linux, Unix › More Linux Help (piping commands)